Skip to content

Commit

Permalink
add correct datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Nov 27, 2024
1 parent a6e2704 commit 2de2485
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 54 deletions.
3 changes: 1 addition & 2 deletions pkg/node/sendoperation/sendoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"strings"

"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/node"
"github.com/massalabs/station/pkg/node/base58"
"github.com/massalabs/station/pkg/node/sendoperation/signer"
Expand Down Expand Up @@ -80,7 +79,7 @@ func Call(
}

content := createOperationContent(operationBatch, description, msgB64, chainID)
logger.Infof("Operation content: %s", content)
// logger.Infof("Operation content: %s", content)

res, err := signer.Sign(nickname, []byte(content))
if err != nil {
Expand Down
171 changes: 124 additions & 47 deletions pkg/onchain/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ import (
"github.com/massalabs/station/pkg/convert"
)

/**
------------------------------------------------------------------------------------------------------------------------
TYPES
------------------------------------------------------------------------------------------------------------------------
*/
type DatastoreContract struct {
Data []byte
Args []byte
Coins uint64
}

type JSONableSlice []byte
type JSONableSliceMap []byte


type DatastoreSCEntry struct {
Entry JSONableSlice `json:"entry"`
Bytes JSONableSlice `json:"bytes"`
Entry DatastoreData `json:"entry"`
Bytes DatastoreData `json:"bytes"`
}

type DatastoreData struct {
Entry JSONableSliceMap `json:"entry"`
Bytes JSONableSliceMap `json:"bytes"`
}

func (u JSONableSlice) MarshalJSON() ([]byte, error) {
func (u JSONableSliceMap) MarshalJSON() ([]byte, error) {
var result string
if u == nil {
result = "null"
Expand All @@ -31,25 +42,46 @@ func (u JSONableSlice) MarshalJSON() ([]byte, error) {
return []byte(result), nil
}

/**
------------------------------------------------------------------------------------------------------------------------
UTILITY FUNCTIONS
------------------------------------------------------------------------------------------------------------------------
*/

/**
* Used to create a new datastore entry object.
*/
func NewDeployScDatastoreData(entry []byte, bytes []byte) DatastoreData {
return DatastoreData{
Entry: entry,
Bytes: bytes,
}
}

func NewDeployScDatastoreEntry(entry []byte, bytes []byte) DatastoreSCEntry {
/**
use to create a map of each datastore entry
*/
func NewDeployScDatastoreEntry(entry DatastoreData, bytes DatastoreData) DatastoreSCEntry {
return DatastoreSCEntry{
Entry: entry,
Bytes: bytes,
}
}

/**
------------------------------------------------------------------------------------------------------------------------
KEY GENERATION FUNCTIONS
------------------------------------------------------------------------------------------------------------------------
*/

/**
* Generates a key for coin data in the datastore.
*
* @param offset - The offset to use when generating the key.
* @returns A Uint8Array representing the key.
*/
func coinsKey(offset int) []byte {
byteArray := []byte{}
byteArray = append(byteArray, convert.U64ToBytes(offset+1)...)
byteArray = append(byteArray, []byte{1}...)
return byteArray
return convert.U64ToBytes(offset+1)
}

/**
Expand All @@ -59,10 +91,7 @@ func coinsKey(offset int) []byte {
* @returns A Uint8Array representing the key.
*/
func argsKey(offset int) []byte {
byteArray := []byte{}
byteArray = append(byteArray, convert.U64ToBytes(offset+1)...)
byteArray = append(byteArray, []byte{0}...)
return byteArray
return convert.U64ToBytes(offset+1)
}

/**
Expand All @@ -72,35 +101,16 @@ func argsKey(offset int) []byte {
* @returns A Uint8Array representing the key.
*/
func contractKey(offset int) []byte {
byteArray := []byte{}
return append(byteArray, convert.U64ToBytes(offset+1)...)
return convert.U64ToBytes(offset+1)
}

// TODO implement correct datastore structure

/**
* Populates the datastore with the contracts.
*
* @remarks
* This function is to be used in conjunction with the deployer smart contract.
* The deployer smart contract expects to have an execution datastore in a specific state.
* This function populates the datastore according to that expectation.
*
* @param contracts - The contracts to populate the datastore with.
*
* @returns The populated datastore.
*/
func populateDatastore(contracts []DatastoreContract) ([]DatastoreSCEntry, error) {
if len(contracts) == 0 {
return nil, fmt.Errorf("contracts slice is empty with a length of: %v", len(contracts))
}
------------------------------------------------------------------------------------------------------------------------
POPULATE DATASTORE FUNCTION
------------------------------------------------------------------------------------------------------------------------
// number of entries in the datastore: number of contracts, and the contract data, args, and coins
datastore := make([]DatastoreSCEntry, 4)

contractsNumberKey := []byte{0}
/**
// data store entry
--- Datastore Format ---
[[key],[value]]
===
Expand All @@ -113,18 +123,85 @@ func populateDatastore(contracts []DatastoreContract) ([]DatastoreSCEntry, error
[VALUE_BYTE_ARRAY_LENGTH], [VALUE_BYTE_ARRAY_DATA]
]
]
*/
// length of the byte array | byte array data
// length of the byte array | byte array data
datastore[0] = NewDeployScDatastoreEntry(convert.U64ToBytes(len(contractsNumberKey)), contractsNumberKey)
*/

for i, contract := range contracts {
datastore[1] = NewDeployScDatastoreEntry(contractKey(i), contract.Data)
datastore[2] = NewDeployScDatastoreEntry(argsKey(i), contract.Args)
datastore[3] = NewDeployScDatastoreEntry(coinsKey(i), convert.U64ToBytes(int(contract.Coins)))
}
/**
* Populates the datastore with the contracts.
*
* @remarks
* This function is to be used in conjunction with the deployer smart contract.
* The deployer smart contract expects to have an execution datastore in a specific state.
* This function populates the datastore according to that expectation.
*
* @param contracts - The contracts to populate the datastore with.
*
* @returns The populated datastore.
*/
func populateDatastore(contract DatastoreContract) ([]DatastoreSCEntry, error) {
//TODO bug -> four empty datastore entries

// IMPORTANT we assume ATM that there is only one contract to deploy

return datastore, nil
// number of entries in the datastore: number of contracts, and the contract data, args, and coins
datastore := []DatastoreSCEntry{}

// contractsNumberKey := []byte{0}
contractlength := convert.U64ToBytes(1) // assuming there is one contract to deploy

//number of contracts to deploy
numberOfContracts := NewDeployScDatastoreEntry(
NewDeployScDatastoreData(
convert.U64ToBytes(len(convert.U64ToBytes(1))), // length of the key
convert.U64ToBytes(1)), // value in bytes
NewDeployScDatastoreData(
convert.U64ToBytes(len(contractlength)),
convert.U64ToBytes(1),
))

_dataStore := append(datastore, numberOfContracts)

//byteCode of the smartContract to be appended to the deployer
contractData := NewDeployScDatastoreEntry(
NewDeployScDatastoreData(
convert.U64ToBytes(len(contractKey(0))),
contractKey(0),
),
NewDeployScDatastoreData(
convert.U64ToBytes(len(contract.Data)),
contract.Data,
),
)

_dataStore = append(_dataStore, contractData)


contractArgs := NewDeployScDatastoreEntry(
NewDeployScDatastoreData(
convert.U64ToBytes(len(argsKey(0))),
argsKey(0),
),
NewDeployScDatastoreData(
convert.U64ToBytes(len(contract.Args)),
contract.Args,
),
)

_dataStore = append(_dataStore, contractArgs)

contractCoins := NewDeployScDatastoreEntry(
NewDeployScDatastoreData(
convert.U64ToBytes(len(coinsKey(0))),
coinsKey(0),
),
NewDeployScDatastoreData(
convert.U64ToBytes(len(convert.U64ToBytes(int(contract.Coins)))),
convert.U64ToBytes(int(contract.Coins)),
),
)

_dataStore = append(_dataStore, contractCoins)

return _dataStore, nil
}

10 changes: 5 additions & 5 deletions pkg/onchain/sc.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,24 @@ func DeploySC(
client := node.NewClient(networkInfos.NodeURL)

// TODO implement populate datastore function
contracts := []DatastoreContract{
{
contract := DatastoreContract{
Data: smartContractByteCode,
Args: parameters,
Coins: coins,
},
}

dataStore, err := populateDatastore(contracts)
dataStore, err := populateDatastore(contract)
if err != nil {
return nil, nil, fmt.Errorf("populating datastore: %w", err)
}

logger.Infof("Datastore: %+v", dataStore)
// logger.Infof("Datastore: %+v", dataStore)


marshaledDataStore, err := json.Marshal(dataStore)

logger.Infof("Datastore: %s", marshaledDataStore)

exeSC := executesc.New(
deployerByteCode,
maxGas,
Expand Down

0 comments on commit 2de2485

Please sign in to comment.