Skip to content

Commit

Permalink
Interim save
Browse files Browse the repository at this point in the history
  • Loading branch information
BugFreeSoftware committed Dec 28, 2021
1 parent 2921b99 commit 5993c65
Show file tree
Hide file tree
Showing 25 changed files with 581 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class FairRouletteService {
balances: [{ balance: take, color: Colors.IOTA_COLOR_BYTES }],
contract: HName.HashAsNumber(this.scName),
entrypoint: HName.HashAsNumber(this.scPlaceBet),
noonce: BigInt(performance.now() + performance.timeOrigin * 10000000),
nonce: BigInt(performance.now() + performance.timeOrigin * 10000000),
};

betRequest = OffLedger.Sign(betRequest, keyPair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export interface Balance {

export interface IOffLedger {
requestType?: number;
chainID: Buffer;
contract: number;
entrypoint: number;
arguments: OffLedgerArgument[];
noonce: bigint;
nonce: bigint;
balances: Balance[];

// Public Key and Signature will get set in the Sign function, so no inital set is required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type { IOffLedger } from './IOffLedger';

export class OffLedger {
public static ToStruct(buffer: Buffer): IOffLedger {
const chainIDSize = 32;
const publicKeySize = 32;
const colorLength = 32;
const signatureSize = 64;

const reader = new SimpleBufferCursor(buffer);

const requestType = reader.readIntBE(1);
const chainID = reader.readBytes(chainIDSize);
const contract = reader.readUInt32LE();
const entrypoint = reader.readUInt32LE();
const numArguments = reader.readUInt32LE();
Expand Down Expand Up @@ -46,11 +48,12 @@ export class OffLedger {

const offLedgerStruct: IOffLedger = {
requestType: requestType,
chainID: Buffer.from(chainID),
contract: contract,
entrypoint: entrypoint,
arguments: args,
publicKey: Buffer.from(publicKey),
noonce: noonce,
nonce: noonce,
balances: balances,
signature: Buffer.from(signature),
};
Expand All @@ -61,15 +64,15 @@ export class OffLedger {
public static ToBuffer(req: IOffLedger): Buffer {
const buffer = new SimpleBufferCursor(Buffer.alloc(0));

if ([0, 1].includes(req.requestType)) {
buffer.writeIntBE(req.requestType, 1);
if (req.requestType) {
buffer.writeInt8(1);
}

buffer.writeBytes(req.chainID);
buffer.writeUInt32LE(req.contract);
buffer.writeUInt32LE(req.entrypoint);

buffer.writeUInt32LE(req.arguments.length || 0);

if (req.arguments) {
req.arguments.sort((lhs,rhs)=> lhs.key.localeCompare(rhs.key));
for (const arg of req.arguments) {
Expand All @@ -78,6 +81,7 @@ export class OffLedger {
buffer.writeUInt16LE(keyBuffer.length);
buffer.writeBytes(keyBuffer);

//TODO FIXME: always a number?
const valueBuffer = Buffer.alloc(8);
valueBuffer.writeInt32LE(arg.value, 0);

Expand All @@ -87,10 +91,9 @@ export class OffLedger {
}

buffer.writeBytes(req.publicKey);
buffer.writeUInt64LE(req.noonce);
buffer.writeUInt64LE(req.nonce);

buffer.writeUInt32LE(req.balances.length || 0);

if (req.balances) {
for (const balance of req.balances) {
buffer.writeBytes(balance.color);
Expand All @@ -111,9 +114,10 @@ export class OffLedger {
const cleanCopyOfRequest: IOffLedger = {
arguments: request.arguments,
balances: request.balances,
chainID: request.chainID,
contract: request.contract,
entrypoint: request.entrypoint,
noonce: request.noonce,
nonce: request.nonce,
publicKey: keyPair.publicKey,

requestType: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export class OnLedger {
const numArguments = reader.readUInt32LE();

const args = [];

for (let i = 0; i < numArguments; i++) {
const sz16 = reader.readUInt16LE();
const key = reader.readBytes(sz16);
const sz32 = reader.readUInt32LE();
const value = reader.readBytes(sz32);

args.push({ key: key, value: value });
}

Expand All @@ -40,7 +38,6 @@ export class OnLedger {
buffer.writeBytes(Buffer.alloc(1, 0));

buffer.writeUInt32LE(req.arguments.length || 0);

if (req.arguments) {
req.arguments.sort((lhs,rhs)=> lhs.key.localeCompare(rhs.key));
for (const arg of req.arguments) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/wasm/testwasmlib/go/testwasmlib/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "github.com/iotaledger/wasp/packages/vm/wasmlib/go/wasmlib"

const (
ScName = "testwasmlib"
ScDescription = "Exercise all aspects of WasmLib"
ScDescription = "Exercise several aspects of WasmLib"
HScName = wasmlib.ScHname(0x89703a45)
)

Expand Down
65 changes: 65 additions & 0 deletions contracts/wasm/testwasmlib/go/testwasmlib/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

// (Re-)generated by schema tool
// >>>> DO NOT CHANGE THIS FILE! <<<<
// Change the json schema instead

package testwasmlib

import "github.com/iotaledger/wasp/packages/vm/wasmlib/go/wasmlib"

type Location struct {
X int32
Y int32
}

func NewLocationFromBytes(bytes []byte) *Location {
decode := wasmlib.NewBytesDecoder(bytes)
data := &Location{}
data.X = decode.Int32()
data.Y = decode.Int32()
decode.Close()
return data
}

func (o *Location) Bytes() []byte {
return wasmlib.NewBytesEncoder().
Int32(o.X).
Int32(o.Y).
Data()
}

type ImmutableLocation struct {
objID int32
keyID wasmlib.Key32
}

func (o ImmutableLocation) Exists() bool {
return wasmlib.Exists(o.objID, o.keyID, wasmlib.TYPE_BYTES)
}

func (o ImmutableLocation) Value() *Location {
return NewLocationFromBytes(wasmlib.GetBytes(o.objID, o.keyID, wasmlib.TYPE_BYTES))
}

type MutableLocation struct {
objID int32
keyID wasmlib.Key32
}

func (o MutableLocation) Delete() {
wasmlib.DelKey(o.objID, o.keyID, wasmlib.TYPE_BYTES)
}

func (o MutableLocation) Exists() bool {
return wasmlib.Exists(o.objID, o.keyID, wasmlib.TYPE_BYTES)
}

func (o MutableLocation) SetValue(value *Location) {
wasmlib.SetBytes(o.objID, o.keyID, wasmlib.TYPE_BYTES, value.Bytes())
}

func (o MutableLocation) Value() *Location {
return NewLocationFromBytes(wasmlib.GetBytes(o.objID, o.keyID, wasmlib.TYPE_BYTES))
}
6 changes: 3 additions & 3 deletions contracts/wasm/testwasmlib/go/testwasmlibclient/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ type TestWasmLibService struct {
wasmclient.Service
}

func NewTestWasmLibService(cl *wasmclient.ServiceClient, chainID string) *TestWasmLibService {
func NewTestWasmLibService(cl *wasmclient.ServiceClient, chainID string) (*TestWasmLibService, error) {
s := &TestWasmLibService{}
s.Service.Init(cl, chainID, 0x89703a45, EventHandlers)
return s
err := s.Service.Init(cl, chainID, 0x89703a45, EventHandlers)
return s, err
}

func (s *TestWasmLibService) ArrayClear() ArrayClearFunc {
Expand Down
36 changes: 34 additions & 2 deletions contracts/wasm/testwasmlib/schema.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
name: TestWasmLib
description: Exercise all aspects of WasmLib
description: Exercise several aspects of WasmLib

# ##################################
events:
# ##################################
test:
name: String
address: Address
structs: {}

# ##################################
structs:
# ##################################
# Location:
# x: Int32
# y: Int32

# ##################################
typedefs:
# ##################################
StringArray: String[]
# Longitude: map[Int32]Location

# ##################################
state:
# ##################################
arrays: map[String]StringArray
random: Int64
# latLong: map[Int32]Longitude

# ##################################
funcs:
# ##################################
arrayClear:
params:
name: String

arrayCreate:
params:
name: String

arraySet:
params:
index: Int32
name: String
value: String

paramTypes:
params:
address: Address?
Expand All @@ -43,37 +66,46 @@ funcs:
uint16: Uint16?
uint32: Uint32?
uint64: Uint64?

random:
triggerEvent:
params:
name: String
address: Address

# ##################################
views:
# ##################################
arrayLength:
params:
name: String
results:
length: Int32

arrayValue:
params:
index: Int32
name: String
results:
value: String

blockRecord:
params:
blockIndex: Int32
recordIndex: Int32
results:
record: Bytes

blockRecords:
params:
blockIndex: Int32
results:
count: Int32

iotaBalance:
results:
iotas: Int64

getRandom:
results:
random: Int64
2 changes: 1 addition & 1 deletion contracts/wasm/testwasmlib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use wasmlib::*;

pub const SC_NAME : &str = "testwasmlib";
pub const SC_DESCRIPTION : &str = "Exercise all aspects of WasmLib";
pub const SC_DESCRIPTION : &str = "Exercise several aspects of WasmLib";
pub const HSC_NAME : ScHname = ScHname(0x89703a45);

pub const PARAM_ADDRESS : &str = "address";
Expand Down
73 changes: 73 additions & 0 deletions contracts/wasm/testwasmlib/src/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

// (Re-)generated by schema tool
// >>>> DO NOT CHANGE THIS FILE! <<<<
// Change the json schema instead

#![allow(dead_code)]
#![allow(unused_imports)]

use wasmlib::*;
use wasmlib::host::*;
use crate::typedefs::*;

pub struct Location {
pub x : i32,
pub y : i32,
}

impl Location {
pub fn from_bytes(bytes: &[u8]) -> Location {
let mut decode = BytesDecoder::new(bytes);
Location {
x : decode.int32(),
y : decode.int32(),
}
}

pub fn to_bytes(&self) -> Vec<u8> {
let mut encode = BytesEncoder::new();
encode.int32(self.x);
encode.int32(self.y);
return encode.data();
}
}

pub struct ImmutableLocation {
pub(crate) obj_id: i32,
pub(crate) key_id: Key32,
}

impl ImmutableLocation {
pub fn exists(&self) -> bool {
exists(self.obj_id, self.key_id, TYPE_BYTES)
}

pub fn value(&self) -> Location {
Location::from_bytes(&get_bytes(self.obj_id, self.key_id, TYPE_BYTES))
}
}

pub struct MutableLocation {
pub(crate) obj_id: i32,
pub(crate) key_id: Key32,
}

impl MutableLocation {
pub fn delete(&self) {
del_key(self.obj_id, self.key_id, TYPE_BYTES);
}

pub fn exists(&self) -> bool {
exists(self.obj_id, self.key_id, TYPE_BYTES)
}

pub fn set_value(&self, value: &Location) {
set_bytes(self.obj_id, self.key_id, TYPE_BYTES, &value.to_bytes());
}

pub fn value(&self) -> Location {
Location::from_bytes(&get_bytes(self.obj_id, self.key_id, TYPE_BYTES))
}
}
Loading

0 comments on commit 5993c65

Please sign in to comment.