-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state migration
- Loading branch information
Showing
12 changed files
with
261 additions
and
8 deletions.
There are no files selected for viewing
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,38 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
const contract = await root.devDeploy("./build/state-migration-original.wasm"); | ||
|
||
const ali = await root.createSubAccount("ali"); | ||
|
||
t.context.worker = worker; | ||
t.context.accounts = { root, contract, ali }; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("migration works", async (t) => { | ||
const { contract, ali } = t.context.accounts; | ||
|
||
await ali.call(contract, "addMessage", { message: { sender: "ali", header: "h1", text: "hello" } }); | ||
await ali.call(contract, "addMessage", { message: { sender: "ali", header: "h2", text: "world" } }); | ||
await ali.call(contract, "addMessage", { message: { sender: "ali", header: "h3", text: "This message is too log for new standard" } }); | ||
await ali.call(contract, "addMessage", { message: { sender: "ali", header: "h4", text: "!" } }); | ||
|
||
const res1 = await contract.view("countMessages", {}); | ||
t.is(res1, 4); | ||
|
||
contract.deploy("./build/state-migration-new.wasm"); | ||
|
||
await ali.call(contract, "migrateState", {}); | ||
|
||
const res2 = await contract.view("countMessages", {}); | ||
t.is(res2, 3); | ||
}); |
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,71 @@ | ||
import { NearBindgen, view, near, migrate, call, Vector, assert } from 'near-sdk-js' | ||
import { AccountId } from 'near-sdk-js/lib/types' | ||
|
||
type OldMessageFormat = { | ||
sender: AccountId, | ||
header: string, | ||
text: string, | ||
} | ||
|
||
// This is the new version of the Message type, it has an additional field | ||
type NewMessageFormat = { | ||
sender: AccountId, | ||
recipient?: AccountId, | ||
header: string, | ||
text: string, | ||
} | ||
|
||
@NearBindgen({}) | ||
export class MigrationDemo { | ||
messages: Vector<NewMessageFormat>; | ||
|
||
constructor() { | ||
this.messages = new Vector<NewMessageFormat>('messages'); | ||
} | ||
|
||
@call({ payableFunction: true }) | ||
addMessage({ message }: { | ||
message: NewMessageFormat | ||
}): void { | ||
this.messages.push(message); | ||
near.log(`${near.signerAccountId()} added message ${JSON.stringify(message)}`); | ||
} | ||
|
||
@view({}) | ||
countMessages(): number { | ||
return this.messages.toArray().length; | ||
} | ||
|
||
|
||
@migrate({}) | ||
migrateState(): Vector<NewMessageFormat> { | ||
assert(this.messages.toArray().length == 0, "Contract state should not be deserialized in @migrate"); | ||
// retrieve the current state from the contract | ||
let raw_vector = JSON.parse(near.storageRead("STATE")).messages; | ||
let old_messages: Vector<OldMessageFormat> = new Vector<OldMessageFormat>(raw_vector.prefix, raw_vector.length); | ||
near.log("old_messages: " + JSON.stringify(old_messages)); | ||
|
||
// iterate through the state migrating it to the new version | ||
let new_messages: Vector<NewMessageFormat> = new Vector('messages'); | ||
|
||
for (let old_message of old_messages) { | ||
near.log(`migrating ${JSON.stringify(old_message)}`); | ||
const new_message: NewMessageFormat = { | ||
sender: old_message.sender, | ||
recipient: "Unknown", | ||
header: old_message.header, | ||
text: old_message.text, | ||
}; | ||
if (new_message.text.length < 10) { | ||
near.log(`adding ${new_message} to new_messages`); | ||
new_messages.push(new_message); | ||
} else { | ||
near.log(`${new_message} is too long, skipping`); | ||
} | ||
} | ||
|
||
this.messages = new_messages; | ||
|
||
return this.messages; | ||
} | ||
} |
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,30 @@ | ||
import { NearBindgen, view, near, migrate, call, Vector, assert } from 'near-sdk-js' | ||
import { AccountId } from 'near-sdk-js/lib/types' | ||
|
||
type Message = { | ||
sender: AccountId, | ||
header: string, | ||
text: string, | ||
} | ||
|
||
@NearBindgen({}) | ||
export class MigrationDemo { | ||
messages: Vector<Message>; | ||
|
||
constructor() { | ||
this.messages = new Vector<Message>('messages'); | ||
} | ||
|
||
@call({ payableFunction: true }) | ||
addMessage({ message }: { | ||
message: Message | ||
}): void { | ||
this.messages.push(message); | ||
near.log(`${near.signerAccountId()} added message ${JSON.stringify(message)}`); | ||
} | ||
|
||
@view({}) | ||
countMessages(): number { | ||
return this.messages.toArray().length; | ||
} | ||
} |
13 changes: 9 additions & 4 deletions
13
packages/near-sdk-js/lib/cli/build-tools/near-bindgen-exporter.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,39 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
const counter = await root.devDeploy("./build/migrate.wasm"); | ||
|
||
const ali = await root.createSubAccount("ali"); | ||
|
||
t.context.worker = worker; | ||
t.context.accounts = { root, counter, ali }; | ||
}); | ||
|
||
// If the environment is reused, use test.after to replace test.afterEach | ||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("migration works", async (t) => { | ||
const { counter, ali } = t.context.accounts; | ||
|
||
const res1 = await counter.view("getCount", {}); | ||
t.is(res1, 0); | ||
|
||
await ali.call(counter, "increase", {}); | ||
|
||
const res2 = await counter.view("getCount", {}); | ||
t.is(res2, 1); | ||
|
||
const migrationRes = await ali.callRaw(counter, "migrFuncValueTo18", {}); | ||
|
||
t.is(JSON.stringify(migrationRes).includes("Count: 0"), true); | ||
|
||
const res3 = await counter.view("getCount", {}); | ||
t.is(res3, 18); | ||
}); |
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,23 @@ | ||
import { NearBindgen, near, call, view, migrate } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class Counter { | ||
count = 0; | ||
|
||
@call({}) | ||
increase({ n = 1 }: { n: number }) { | ||
this.count += n; | ||
near.log(`Counter increased to ${this.count}`); | ||
} | ||
|
||
@view({}) | ||
getCount(): number { | ||
return this.count; | ||
} | ||
|
||
@migrate({}) | ||
migrFuncValueTo18(): void { | ||
near.log("Count: " + this.count); // expected to be 0 | ||
this.count = 18; | ||
} | ||
} |