-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Terran-One/feat/persist
Persistence Update
- Loading branch information
Showing
10 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { toBase64 } from '@cosmjs/encoding'; | ||
import fs from 'fs'; | ||
import { CWSimulateApp } from './CWSimulateApp'; | ||
import * as persist from './persist'; | ||
import { TestContract } from '../testing/wasm-util'; | ||
|
||
const bytecode = fs.readFileSync('./testing/cw_simulate_tests-aarch64.wasm'); | ||
|
||
describe('de/serialize', () => { | ||
it('works', async () => { | ||
{ | ||
const ref = new CWSimulateApp({ chainId: 'phoenix-1', bech32Prefix: 'terra1' }); | ||
ref.wasm.create('alice', bytecode); | ||
ref.wasm.create('bob', bytecode); | ||
|
||
const response = await ref.wasm.instantiateContract('alice', [], 1, {}, ''); | ||
const address = response.unwrap().events[0].attributes[0].value; | ||
|
||
const bytes = persist.save(ref); | ||
const clone = await persist.load(bytes); | ||
expect(clone.chainId).toStrictEqual(ref.chainId); | ||
expect(clone.bech32Prefix).toStrictEqual(ref.bech32Prefix); | ||
|
||
const code1 = clone.wasm.getCodeInfo(1)!; | ||
const code2 = clone.wasm.getCodeInfo(2)!; | ||
expect(code1.creator).toStrictEqual('alice'); | ||
expect(code2.creator).toStrictEqual('bob'); | ||
expect(toBase64(code1.wasmCode)).toStrictEqual(toBase64(ref.wasm.store.getObject('codes', 1, 'wasmCode'))); | ||
expect(toBase64(code2.wasmCode)).toStrictEqual(toBase64(ref.wasm.store.getObject('codes', 2, 'wasmCode'))); | ||
|
||
let result = await clone.wasm.executeContract('alice', [], address, { debug: { msg: 'foobar' }}); | ||
expect(result.ok).toBeTruthy(); | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export * from './CWSimulateApp'; | ||
export * from './types'; | ||
export * from './store'; | ||
|
||
import { save, load } from './persist'; | ||
export const persist = { save, load }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import SerdeProtocol, { SERDE } from '@kiruse/serde'; | ||
import { Reference } from '@kiruse/serde/dist/types'; | ||
import { List, Map } from 'immutable'; | ||
import { Ok } from 'ts-results'; | ||
import { CWSimulateApp } from './CWSimulateApp'; | ||
|
||
export const serde = SerdeProtocol.standard() | ||
.derive('immutable-list', | ||
(list: List<any>, data) => { | ||
return { | ||
data: data(list.toArray()), | ||
// ownerID is a unique object that should not even appear on | ||
// other Immutable data structures. When present, it signifies | ||
// that the Immutable should be mutated in-place rather than | ||
// creating copies of its data. | ||
mutable: !!(list as any).__ownerID, | ||
}; | ||
}, | ||
({ data, mutable }, deref) => { | ||
if (!data.length) return List(); | ||
const list = List().asMutable(); | ||
Reference.all(deref, data, values => { | ||
for (const value of values) { | ||
list.push(value); | ||
} | ||
!mutable && list.asImmutable(); | ||
}); | ||
return list; | ||
}, | ||
) | ||
.derive('immutable-map', | ||
(map: Map<any, any>, data) => { | ||
return { | ||
data: data(map.toObject()), | ||
// same as with List above | ||
mutable: !!(map as any).__ownerID, | ||
}; | ||
}, | ||
({ data, mutable }, deref) => { | ||
const map = Map().asMutable(); | ||
const keys = Object.keys(data); | ||
if (!keys.length) return Map(); | ||
Reference.all(deref, keys.map(k => data[k]), values => { | ||
values.forEach((value, i) => { | ||
const key = keys[i]; | ||
map.set(key, value); | ||
}); | ||
!mutable && map.asImmutable(); | ||
}); | ||
return map; | ||
}, | ||
) | ||
.derive('cw-simulate-app', | ||
(app: CWSimulateApp) => ({ | ||
chainId: app.chainId, | ||
bech32Prefix: app.bech32Prefix, | ||
store: app.store.db.data, | ||
}), | ||
({ chainId, bech32Prefix, store }, deref): CWSimulateApp => { | ||
const app = new CWSimulateApp({ | ||
chainId, | ||
bech32Prefix, | ||
}); | ||
Reference.all(deref, [store], ([map]) => { | ||
app.store.db.tx(update => { | ||
update(() => map); | ||
return Ok(undefined); | ||
}); | ||
}); | ||
return app; | ||
}, | ||
) | ||
|
||
export const save = (app: CWSimulateApp) => serde.serializeAs('cw-simulate-app', app).compress().buffer; | ||
export const load = async (bytes: Uint8Array) => { | ||
const app = serde.deserializeAs('cw-simulate-app', bytes); | ||
const contracts = [...app.wasm.store.get('contracts').keys()]; | ||
await Promise.all(contracts.map(address => app.wasm.buildVM(address))); | ||
return app; | ||
}; | ||
|
||
// Inject SERDE | ||
Map.prototype[SERDE] = 'immutable-map'; | ||
List.prototype[SERDE] = 'immutable-list'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters