Skip to content

Commit

Permalink
argsKey and coinsKey for populate datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed May 3, 2024
1 parent afb13e8 commit ba3065f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*
*/
import { BlockchainClient } from '../client'
import { MAX_GAS_EXECUTE } from '../smartContract'
import { ZERO } from '../utils'
import { PrivateKey } from './keys'
import { Operation } from './operation'
Expand All @@ -20,6 +19,8 @@ import {
OperationType,
} from './operationManager'

export const MAX_GAS_EXECUTE = 3980167295n

type ExecuteOption = {
fee?: bigint
periodToLive?: number
Expand Down
83 changes: 83 additions & 0 deletions packages/massa-web3/src/experimental/basicElements/dataStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { u64ToBytes, u8toByte } from '@massalabs/web3-utils'
import { ONE, ZERO } from '../utils'
import { Args } from './args'

const CONTRACT_LENGTH_KEY = 0x00

type DatastoreContract = {
data: Uint8Array
args: Uint8Array
coins: bigint
}

/**
* 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.
*/
function coinsKey(offset: number): Uint8Array {
return new Args()
.addU64(BigInt(offset + ONE))
.addUint8Array(u8toByte(ONE))
.serialize()
}

/**
* Generates a key for args data in the datastore.
*
* @param offset - The offset to use when generating the key.
* @returns A Uint8Array representing the key.
*/
function argsKey(offset: number): Uint8Array {
return new Args()
.addU64(BigInt(offset + ONE))
.addUint8Array(u8toByte(ZERO))
.serialize()
}

/**
* Generates a key for contract data in the datastore.
*
* @param offset - The offset to use when generating the key.
* @returns A Uint8Array representing the key.
*/
function contractKey(offset: number): Uint8Array {
return u64ToBytes(BigInt(offset + ONE))
}

/**
* 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.
*/
export function populateDatastore(
contracts: DatastoreContract[]
): Map<Uint8Array, Uint8Array> {
const datastore = new Map<Uint8Array, Uint8Array>()

// set the number of contracts in the first key of the datastore
datastore.set(
new Uint8Array([CONTRACT_LENGTH_KEY]),
u64ToBytes(BigInt(contracts.length))
)

contracts.forEach((contract, i) => {
datastore.set(contractKey(i), contract.data)
if (contract.args) {
datastore.set(argsKey(i), contract.args)
}
if (contract.coins > ZERO) {
datastore.set(coinsKey(i), u64ToBytes(contract.coins))
}
})

return datastore
}
59 changes: 1 addition & 58 deletions packages/massa-web3/src/experimental/smartContract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { u64ToBytes, u8toByte } from '@massalabs/web3-utils'
import {
Operation,
OperationManager,
calculateExpirePeriod,
OperationType,
Address,
CallOperation,
Args,
} from './basicElements'
import * as StorageCost from './basicElements/storage'
import { BlockchainClient } from './client'
Expand All @@ -15,68 +13,13 @@ import { ErrorInsufficientBalance, ErrorMaxGas } from './errors'
import { deployer } from './generated/deployer-bytecode'
import { ONE, ZERO } from './utils'
import { execute } from './basicElements/bytecode'
import { populateDatastore } from './basicElements/dataStore'

// TODO: Move to constants file
export const MAX_GAS_EXECUTE = 3980167295n
export const MAX_GAS_CALL = 4294167295n
export const MIN_GAS_CALL = 2100000n
const MASTER_KEY = 0x00
const DEFAULT_PERIODS_TO_LIVE = 9

type DatastoreContract = {
data: Uint8Array
args: Uint8Array
coins: bigint
}

/**
* 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.
*/
function populateDatastore(
contracts: DatastoreContract[]
): Map<Uint8Array, Uint8Array> {
const datastore = new Map<Uint8Array, Uint8Array>()

// set the number of contracts in the first key of the datastore
datastore.set(
new Uint8Array([MASTER_KEY]),
u64ToBytes(BigInt(contracts.length))
)

contracts.forEach((contract, i) => {
datastore.set(u64ToBytes(BigInt(i + ONE)), contract.data)
if (contract.args) {
datastore.set(
new Args()
.addU64(BigInt(i + ONE))
.addUint8Array(u8toByte(ZERO))
.serialize(),
contract.args
)
}
if (contract.coins > ZERO) {
datastore.set(
new Args()
.addU64(BigInt(i + ONE))
.addUint8Array(u8toByte(ONE))
.serialize(),
u64ToBytes(BigInt(contract.coins))
)
}
})

return datastore
}

type CommonOptions = {
fee?: bigint
maxGas?: bigint
Expand Down

0 comments on commit ba3065f

Please sign in to comment.