Skip to content

Commit fe0b33d

Browse files
committed
fix(chain): state is being stored correctly again
1 parent 153f1b9 commit fe0b33d

File tree

8 files changed

+62
-38
lines changed

8 files changed

+62
-38
lines changed

packages/cosmic-swingset/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ scenario2-run-chain:
8787
ba="$$ba "`cat $$acha`; \
8888
done; \
8989
ROLE=two_chain BOOT_ADDRESS="$$ba" $(NODE_DEBUG) \
90-
`$(BREAK_CHAIN) && echo --inspect-brk` $(AGC) start # --trace-store=trace.log
90+
`$(BREAK_CHAIN) && echo --inspect-brk` $(AGC) start --pruning=nothing
9191
scenario2-run-client:
9292
cd t1/$(BASE_PORT) && ../../bin/ag-solo start --role=two_client
9393

packages/cosmic-swingset/lib/ag-chain-cosmos

+6-1
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,14 @@ async function toSwingSet0(action, _replier) {
161161
action.ack,
162162
action.blockHeight,
163163
action.blockTime,
164+
action.committed,
164165
);
165166
case BEGIN_BLOCK:
166-
return deliverStartBlock(action.blockHeight, action.blockTime);
167+
return deliverStartBlock(
168+
action.blockHeight,
169+
action.blockTime,
170+
action.committed,
171+
);
167172
default:
168173
throw new Error(`${action.type} not recognized. must be DELIVER_INBOUND or BEGIN_BLOCK`);
169174
}

packages/cosmic-swingset/lib/agcosmosdaemon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ func ReplyToGo(replyPort C.int, isError C.int, str C.Body) C.int {
9898
func SendToGo(port C.int, str C.Body) C.Body {
9999
goStr := C.GoString(str)
100100
// fmt.Fprintln(os.Stderr, "Send to Go", goStr)
101-
outstr, err := swingset.ReceiveFromNode(int(port), goStr)
101+
outstr, err := swingset.ReceiveFromController(int(port), goStr)
102102
if err != nil {
103-
fmt.Fprintln(os.Stderr, "Cannot receive from node", err)
103+
fmt.Fprintln(os.Stderr, "Cannot receive from controller", err)
104104
return C.CString("")
105105
}
106106
return C.CString(outstr)

packages/cosmic-swingset/lib/launch-chain.js

+45-23
Original file line numberDiff line numberDiff line change
@@ -70,56 +70,78 @@ export async function launch(kernelStateDBDir, mailboxStorage, vatsDir, argv) {
7070
);
7171

7272
let mailboxLastData = djson.stringify(mbs.exportToData());
73-
// save the initial state immediately
74-
commit();
75-
76-
// then arrange for inbound messages to be processed, after which we save
77-
async function turnCrank() {
73+
function saveState(runTime = undefined) {
7874
let start = Date.now();
79-
await controller.run();
80-
const runTime = Date.now() - start;
75+
8176
// now check mbs
82-
start = Date.now();
8377
const newState = mbs.exportToData();
8478
const newData = djson.stringify(newState);
8579
if (newData !== mailboxLastData) {
8680
console.log(`outbox changed`);
87-
for (const peer of Object.getOwnPropertyNames(newState)) {
88-
const data = {
89-
outbox: newState[peer].outbox,
90-
ack: newState[peer].inboundAck,
91-
};
92-
mailboxStorage.set(`mailbox.${peer}`, djson.stringify(data));
93-
}
94-
mailboxStorage.set(`mailbox`, newData);
95-
mailboxLastData = newData;
9681
}
82+
83+
for (const peer of Object.getOwnPropertyNames(newState)) {
84+
const data = {
85+
outbox: newState[peer].outbox,
86+
ack: newState[peer].inboundAck,
87+
};
88+
mailboxStorage.set(`mailbox.${peer}`, djson.stringify(data));
89+
}
90+
mailboxStorage.set('mailbox', newData);
91+
mailboxLastData = newData;
92+
9793
const mbTime = Date.now() - start;
94+
95+
// Save the rest of the kernel state.
9896
start = Date.now();
99-
const mailboxSize = mailboxLastData.length;
97+
commit();
10098
const saveTime = Date.now() - start;
99+
100+
const mailboxSize = mailboxLastData.length;
101+
const runTimeStr = runTime === undefined ? '' : `run=${runTime}ms, `;
101102
console.log(
102-
`wrote SwingSet checkpoint (mailbox=${mailboxSize}), [run=${runTime}ms, mb=${mbTime}ms, save=${saveTime}ms]`,
103+
`wrote SwingSet checkpoint (mailbox=${mailboxSize}), [${runTimeStr}mb=${mbTime}ms, save=${saveTime}ms]`,
103104
);
104105
}
105106

106-
async function deliverInbound(sender, messages, ack) {
107+
// save the initial state immediately
108+
saveState();
109+
110+
// then arrange for inbound messages to be processed, after which we save
111+
async function turnCrank(committed) {
112+
const start = Date.now();
113+
await controller.run();
114+
const runTime = Date.now() - start;
115+
if (committed) {
116+
saveState(runTime);
117+
} else {
118+
console.log(`proposed SwingSet transaction [run=${runTime}ms]`);
119+
}
120+
}
121+
122+
async function deliverInbound(sender, messages, ack, committed) {
107123
if (!(messages instanceof Array)) {
108124
throw new Error(`inbound given non-Array: ${messages}`);
109125
}
110126
if (mb.deliverInbound(sender, messages, ack)) {
111127
console.log(`mboxDeliver: ADDED messages`);
112-
await turnCrank();
128+
await turnCrank(committed);
129+
} else if (committed) {
130+
// We need to save our state on every commitment.
131+
saveState();
113132
}
114133
}
115134

116-
async function deliverStartBlock(blockHeight, blockTime) {
135+
async function deliverStartBlock(blockHeight, blockTime, committed) {
117136
const addedToQueue = timer.poll(blockTime);
118137
console.log(
119138
`polled; blockTime:${blockTime}, h:${blockHeight} ADDED: ${addedToQueue}`,
120139
);
121140
if (addedToQueue) {
122-
await turnCrank();
141+
await turnCrank(committed);
142+
} else if (committed) {
143+
// We need to save our state on every commitment.
144+
saveState();
123145
}
124146
}
125147

packages/cosmic-swingset/s2.sh

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ EOF
99
ag-cosmos-helper tx swingset deliver --keyring-backend=test $addr @/tmp/s2.json --gas=auto --gas-adjustment=1.05 --from=ag-solo -ojson --broadcast-mode=block --yes --output=json $opts
1010
fi
1111
ag-cosmos-helper query swingset mailbox $addr $opts
12+
#ag-cosmos-helper query swingset mailbox ${addr}z $opts

packages/cosmic-swingset/x/swingset/handler.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ type deliverInboundAction struct {
2121
StoragePort int `json:"storagePort"`
2222
BlockHeight int64 `json:"blockHeight"`
2323
BlockTime int64 `json:"blockTime"`
24+
Committed bool `json:"committed"`
2425
}
2526

2627
type beginBlockAction struct {
2728
Type string `json:"type"`
2829
StoragePort int `json:"storagePort"`
2930
BlockHeight int64 `json:"blockHeight"`
3031
BlockTime int64 `json:"blockTime"`
32+
Committed bool `json:"committed"`
3133
}
3234

3335
// NewHandler returns a handler for "swingset" type messages.
@@ -64,7 +66,7 @@ func UnregisterPortHandler(portNum int) error {
6466
return nil
6567
}
6668

67-
func ReceiveFromNode(portNum int, msg string) (string, error) {
69+
func ReceiveFromController(portNum int, msg string) (string, error) {
6870
handler := portToHandler[portNum]
6971
if handler == nil {
7072
return "", errors.New("Unregistered port " + fmt.Sprintf("%d", portNum))
@@ -101,7 +103,9 @@ func handleMsgDeliverInbound(ctx sdk.Context, keeper Keeper, msg MsgDeliverInbou
101103
StoragePort: newPort,
102104
BlockHeight: ctx.BlockHeight(),
103105
BlockTime: ctx.BlockTime().Unix(),
106+
Committed: !ctx.IsCheckTx(),
104107
}
108+
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)
105109
b, err := json.Marshal(action)
106110
if err != nil {
107111
return nil, err
@@ -129,6 +133,7 @@ func handleMsgBeginBlock(ctx sdk.Context, keeper Keeper) (*sdk.Result, error) {
129133
BlockHeight: ctx.BlockHeight(),
130134
BlockTime: ctx.BlockTime().Unix(),
131135
StoragePort: newPort,
136+
Committed: !ctx.IsCheckTx(),
132137
}
133138
b, err := json.Marshal(action)
134139
if err != nil {

packages/cosmic-swingset/x/swingset/internal/keeper/keeper.go

-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package keeper
22

33
import (
4-
"fmt"
5-
"os"
64
"sort"
75
"strings"
86

@@ -111,20 +109,13 @@ func (k Keeper) SetStorage(ctx sdk.Context, path string, storage types.Storage)
111109
keyNode.Keys = keyList
112110
keysStore.Set([]byte(oneUp), k.cdc.MustMarshalBinaryLengthPrefixed(keyNode))
113111
}
114-
if addr, ok := os.LookupEnv("SS_DEBUG_MAILBOX"); ok {
115-
fmt.Println("mailbox has value", k.storeKey, store.Has([]byte("data:mailbox."+addr)))
116-
}
117112
}
118113

119114
// Gets the entire mailbox struct for a peer
120115
func (k Keeper) GetMailbox(ctx sdk.Context, peer string) types.Storage {
121116
store := ctx.KVStore(k.storeKey)
122117
dataStore := prefix.NewStore(store, types.DataPrefix)
123118
path := "mailbox." + peer
124-
if addr, ok := os.LookupEnv("SS_DEBUG_MAILBOX"); ok {
125-
fmt.Println("peer == addr", addr == peer)
126-
fmt.Println("mailbox still has value", dataStore.Has([]byte("mailbox."+addr)))
127-
}
128119
if !dataStore.Has([]byte(path)) {
129120
return types.NewMailbox()
130121
}

packages/deployment/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ show-config display the client connection parameters
404404
needReMain([
405405
'play',
406406
'install',
407-
`-eexecline=${shellEscape('/usr/src/cosmic-swingset/lib/ag-chain-cosmos start --proxy_app=kvstore')}`,
407+
`-eexecline=${shellEscape('/usr/src/cosmic-swingset/lib/ag-chain-cosmos start --pruning=nothing')}`,
408408
`-eserviceLines="Environment=BOOT_ADDRESS=${bootAddress}"`,
409409
]),
410410
);

0 commit comments

Comments
 (0)