Skip to content

Commit

Permalink
Merge pull request #89 from casper-ecosystem/hotfix_next/list-fixes
Browse files Browse the repository at this point in the history
Fixes for CLMap implementation 2.2.2
  • Loading branch information
hoffmannjan authored Jul 23, 2021
2 parents 96c217a + 030a25b commit bd25a17
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to casper-js-sdk.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.2.2

### Fixed

- `CLMap` fix for empty maps from bytes
- `CLMap` replaced problematic Map implementation

## 2.2.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "2.2.1",
"version": "2.2.2",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
Expand Down
19 changes: 8 additions & 11 deletions src/lib/CLValue/Map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('CLValue CLMap implementation', () => {
const myMap = new CLMap([[myKey, myVal]]);

expect(myMap).to.be.an.instanceof(CLMap);
expect(myMap.value()).to.be.deep.eq(new Map([[myKey, myVal]]));
expect(myMap).to.be.deep.eq(new CLMap([[myKey, myVal]]));
});

it('Should throw an error when CLMap is not correct by construction', () => {
Expand All @@ -51,7 +51,7 @@ describe('CLValue CLMap implementation', () => {
const myVal = new CLI32(10);
const myMap = new CLMap([[myKey, myVal]]);

expect(myMap.get(myKey)).to.be.deep.eq(myVal);
expect(myMap.get(new CLString('ABC'))).to.be.deep.eq(new CLI32(10));
});

it('Get() should return undefined on non-existing key', () => {
Expand All @@ -77,31 +77,28 @@ describe('CLValue CLMap implementation', () => {

myMap.set(myKey, newVal);

expect(myMap.get(myKey)).to.deep.eq(newVal);
expect(myMap.get(new CLString('ABC'))).to.deep.eq(new CLI32(11));
});

it('Set should be able to set values at already declared keys', () => {
const myKey = new CLString('ABC');
const myVal = new CLI32(10);
const myMap = new CLMap([[myKey, myVal]]);
const newVal = new CLI32(11);

myMap.set(myKey, newVal);
myMap.set(new CLString('ABC'), new CLI32(11));

expect(myMap.get(myKey)).to.deep.eq(newVal);
expect(myMap.get(new CLString('ABC'))).to.deep.eq(new CLI32(11));
expect(myMap.size()).to.eq(1);
});

it('Set should be able to set values at empty keys', () => {
const myKey = new CLString('ABC');
const myVal = new CLI32(10);
const myMap = new CLMap([[myKey, myVal]]);
const newKey = new CLString('DEF');
const newVal = new CLI32(11);

myMap.set(newKey, newVal);
myMap.set(new CLString('DEF'), new CLI32(11));

expect(myMap.get(newKey)).to.deep.eq(new CLI32(11));
expect(myMap.get(new CLString('DEF'))).to.deep.eq(new CLI32(11));
expect(myMap.size()).to.eq(2);
});

Expand All @@ -110,7 +107,7 @@ describe('CLValue CLMap implementation', () => {
const myVal = new CLI32(10);
const myMap = new CLMap([[myKey, myVal]]);

myMap.delete(myKey);
myMap.delete(new CLString('ABC'));

expect(myMap.size()).to.eq(0);
});
Expand Down
39 changes: 24 additions & 15 deletions src/lib/CLValue/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class CLMapBytesParser extends CLValueBytesParsers {
const byteVal = CLValueParsers.toBytes(value).unwrap();
return concat([byteKey, byteVal]);
});
return Ok(concat([toBytesU32(value.data.size), ...kvBytes]));
return Ok(concat([toBytesU32(value.data.length), ...kvBytes]));
}

fromBytesWithRemainder(
Expand All @@ -97,6 +97,13 @@ export class CLMapBytesParser extends CLValueBytesParsers {

let remainder = u32Rem;

if (size === 0) {
return resultHelper(
Ok(new CLMap([mapType.innerKey, mapType.innerValue])),
remainder
);
}

for (let i = 0; i < size; i++) {
if (!remainder) return resultHelper(Err(CLErrorCodes.EarlyEndOfStream));

Expand Down Expand Up @@ -128,7 +135,7 @@ export class CLMapBytesParser extends CLValueBytesParsers {
}

export class CLMap<K extends CLValue, V extends CLValue> extends CLValue {
data: Map<K, V>;
data: [K, V][];
refType: [CLType, CLType];
/**
* Constructs a new `MapValue`.
Expand All @@ -147,13 +154,13 @@ export class CLMap<K extends CLValue, V extends CLValue> extends CLValue {
);
})
) {
this.data = new Map(v);
this.data = v;
} else {
throw Error('Invalid data provided.');
}
} else if (v[0] instanceof CLType && v[1] instanceof CLType) {
this.refType = v;
this.data = new Map();
this.data = [];
} else {
throw Error('Invalid data type(s) provided.');
}
Expand All @@ -163,28 +170,30 @@ export class CLMap<K extends CLValue, V extends CLValue> extends CLValue {
return new CLMapType(this.refType);
}

value(): Map<K, V> {
value(): [K, V][] {
return this.data;
}

get(k: K): V | undefined {
for (const [key, value] of Array.from(this.data.entries())) {
if (key.value() === k.value()) {
return value;
}
}
return undefined;
const result = this.data.find(d => d[0].value() === k.value());
return result ? result[1] : undefined;
}

set(k: K, val: V): void {
this.data.set(k, val);
if (this.get(k)) {
this.data = this.data.map(d =>
d[0].value() === k.value() ? [d[0], val] : d
);
return;
}
this.data = [...this.data, [k, val]];
}

delete(k: K): boolean {
return this.data.delete(k);
delete(k: K): void {
this.data = this.data.filter(d => d[0].value() !== k.value());
}

size(): number {
return this.data.size;
return this.data.length;
}
}
2 changes: 1 addition & 1 deletion src/services/EventStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class EventStream {
const result = parseEvent(Buffer.from(buf).toString());
if (result && !result.err) {
this.subscribedTo.forEach((sub: EventSubscription) => {
if (result.body.hasOwnProperty(sub.eventName)) {
if (result.body && result.body.hasOwnProperty(sub.eventName)) {
sub.eventHandlerFn(result);
}
});
Expand Down

0 comments on commit bd25a17

Please sign in to comment.