Skip to content

Commit

Permalink
Fix kread bundle publishing (#112)
Browse files Browse the repository at this point in the history
## Description

This PR fixes the issue with publishing the kread bundles. To fix it
`agoric published` has been replaced by the `installBundles` script in
the `agoric/contract/scripts/` folder. The script connects to the chain
using Cosmos StargateClient library and sends a transaction to the
correct endpoint with the bundle as payload. This is along the lines of
what happens under the hood of `agoric publish` as well but for some
reason that seems to be broken currently.

For funding the gas fees of the transaction, the same account is used
that funds the transactions to publish the committee bundles and sends
proposals for the kread-committee and kread.

Also, in this PR:
- Added some extra steps to `make kread-committee` (and no-build
version) that fetches the address that is to be funded from the
`AGORIC_SDK_PATH/packages/cosmic-swingset/t1/8000/` folder and funds it.
This way we no longer need to run `kread-committee`, copy the logged
address and fund it.
- Reverted back to the old folder structure where the contract code
lives in `src` instead of the versioned folder structere.

## Related Issues

Fixes #111 

## Checklist

Make sure all items are checked before submitting the pull request.
Remove any items that are not applicable.

- [x] I have used agoric's linter on my code
(Agoric/agoric-sdk#8274)
- [x] I have updated the documentation to reflect the changes (if
applicable).
- [x] I have added/updated unit tests to cover the changes.
- [x] All existing tests pass.
  • Loading branch information
Pandelissym authored Dec 18, 2023
1 parent 064b281 commit 56f72c4
Show file tree
Hide file tree
Showing 37 changed files with 356 additions and 3,143 deletions.
10 changes: 9 additions & 1 deletion agoric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ clean:
# 5. make provision-fee-collector
# 6. start the KREAd contract using 'KREAD_COMMITTEE_NAME='kread' KREAD_COMMITTEE_ADDRESSES='{"voter": "agoric1ersatz"}' make start-kread'
kread-committee: dist/kread-committee-info.json
cd $(COSMIC_SWINGSET_PATH); \
make fund-acct ACCT_ADDR=$$(head t1/8000/ag-cosmos-helper-address) FUNDS=1000000000000uist; \
make fund-acct ACCT_ADDR=$$(head t1/8000/ag-cosmos-helper-address) FUNDS=1000000000000ubld; \
cd $(AG_DIR); \
jq -r '.bundles[]' dist/kread-committee-info.json | sort -u > kread-committee-bundles.out
for b in `cat kread-committee-bundles.out` ; do \
$(AGORIC_CMD) publish --node 127.0.0.1:26657 $$b --chain-id agoriclocal --home $(COSMIC_SWINGSET_PATH)/t1/8000 ; \
Expand All @@ -98,13 +102,17 @@ kread-committee: dist/kread-committee-info.json
start-kread: dist/start-kread-info.json
jq -r '.bundles[]' dist/start-kread-info.json | sort -u > start-kread-bundles.out
for b in `cat start-kread-bundles.out` ; do \
$(AGORIC_CMD) publish --node 127.0.0.1:26657 $$b --chain-id agoriclocal --home $(COSMIC_SWINGSET_PATH)/t1/8000 ; \
node contract/scripts/installBundles.js $(COSMIC_SWINGSET_PATH) $$b ; \
done
cd $(COSMIC_SWINGSET_PATH); \
make scenario2-core-eval EVAL_PERMIT=$(AG_DIR)/dist/start-kread-permit.json \
EVAL_CODE=$(AG_DIR)/dist/start-kread.js EVAL_CLEAN=$(AG_DIR)/dist/start-kread.js.t scenario2-vote VOTE_PROPOSAL=$(NEXT_PROPOSAL) \

kread-committee-no-build:
cd $(COSMIC_SWINGSET_PATH); \
make fund-acct ACCT_ADDR=$$(head t1/8000/ag-cosmos-helper-address) FUNDS=1000000000000uist; \
make fund-acct ACCT_ADDR=$$(head t1/8000/ag-cosmos-helper-address) FUNDS=1000000000000ubld; \
cd $(AG_DIR); \
jq -r '.bundles[]' dist/kread-committee-info.json | sort -u > kread-committee-bundles.out
for b in `cat kread-committee-bundles.out` ; do \
$(AGORIC_CMD) publish --node 127.0.0.1:26657 $$b --chain-id agoriclocal --home $(COSMIC_SWINGSET_PATH)/t1/8000 ; \
Expand Down
13 changes: 3 additions & 10 deletions agoric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ ensure you are in the agoric folder otherwise cd to agoric folder

1. Start the chain
1. make local-testnet
2. Update KEPLR_ADDRESS in Makefile.paths
1. try to `make kread-committee`
2. it will fail but look for `"sender","value":"agoric1` to find the address
1. if it fails for another reason, you may need to run `make reset-client-local-testnet`
3. copy that address into Makefile.paths.local for KEPLR_ADDRESS
3. fund the account
1. make fund-account
4. make the committee
2. make the committee
1. make kread-committee
5. provision the fee collector wallet
3. provision the fee collector wallet
1. make provision-fee-collector
6. start the KREAd contract
4. start the KREAd contract
1. make clean start-kread

To confirm it started,
Expand Down
6 changes: 6 additions & 0 deletions agoric/contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@agoric/assert": "agoric-upgrade-11",
"@agoric/cosmic-proto": "agoric-upgrade-11",
"@agoric/deploy-script-support": "agoric-upgrade-11",
"@agoric/ertp": "agoric-upgrade-11",
"@agoric/governance": "agoric-upgrade-11",
Expand All @@ -44,6 +45,11 @@
"@agoric/vats": "agoric-upgrade-11",
"@agoric/zoe": "agoric-upgrade-11",
"@agoric/zone": "agoric-upgrade-11",
"@cosmjs/crypto": "^0.32.1",
"@cosmjs/encoding": "^0.32.1",
"@cosmjs/math": "^0.32.1",
"@cosmjs/proto-signing": "^0.32.1",
"@cosmjs/stargate": "^0.31.1",
"@endo/bundle-source": "^2.5.2-upstream-rollup",
"@endo/eventual-send": "^0.17.2",
"@endo/far": "^0.2.18",
Expand Down
101 changes: 101 additions & 0 deletions agoric/contract/scripts/installBundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-disable no-undef */
import { fromBech32 } from '@cosmjs/encoding';
import { SigningStargateClient, defaultRegistryTypes } from '@cosmjs/stargate';
import { DirectSecp256k1HdWallet, Registry, coins } from '@cosmjs/proto-signing';
import { Decimal } from '@cosmjs/math';
import { stringToPath } from '@cosmjs/crypto';
import { MsgInstallBundle } from '@agoric/cosmic-proto/swingset/msgs.js';
import * as fs from 'fs';

const RPC = 'http://localhost:26657';

const hdPath = (coinType = 118, account = 0) =>
stringToPath(`m/44'/${coinType}'/${account}'/0/0`);

export const registry = new Registry([
...defaultRegistryTypes,
['/agoric.swingset.MsgInstallBundle', MsgInstallBundle],
]);

const Agoric = {
Bech32MainPrefix: 'agoric',
CoinType: 564,
};

const makeFeeObject = ({ denom, amount, gas }) => ({
amount: coins(amount || 0, denom || 'uist'),
gas: gas ? String(gas) : 'auto',
});

/**
* Gets the wallet from the mnemonic and uses it to connect to the chain using a stargate client
*
* @param {string} walletMnemonic the mnemonic of the wallet that signs the transaction
* @returns A stargate client
*/
const initializeStargateClient = async (walletMnemonic) => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(walletMnemonic, {
prefix: Agoric.Bech32MainPrefix,
hdPaths: [hdPath(Agoric.CoinType, 0), hdPath(Agoric.CoinType, 1)],
});

return SigningStargateClient.connectWithSigner(RPC, wallet, {
registry,
gasPrice: {
denom: 'uist',
amount: Decimal.fromUserInput('50000000', 0),
},
});
};

/**
* Parse bundle, get wallet and send transaction
* over stargate to install the bundle on chain
*/
async function installBundle() {
const cosmicSwingsetPath = process.argv[2];
const bundlePath = process.argv[3];

const bundleText = await fs.readFileSync(bundlePath, 'utf-8');
const walletAddress = (await fs.readFileSync(
`${cosmicSwingsetPath}/t1/8000/ag-cosmos-helper-address`,
'utf-8',
)).trim();
const walletMnemonic = (await fs.readFileSync(
`${cosmicSwingsetPath}/t1/8000/ag-solo-mnemonic`,
'utf-8',
)).trim();

const proposalMsg = {
typeUrl: '/agoric.swingset.MsgInstallBundle',
value: {
bundle: bundleText,
submitter: fromBech32(walletAddress).data,
},
};

const stargateClient = await initializeStargateClient(
walletMnemonic
);

if (!stargateClient) {
throw new Error('stargateClient not found');
}

const estimate = await stargateClient.simulate(
walletAddress,
[proposalMsg],
undefined,
);
const adjustment = 1.3;
const gas = Math.ceil(estimate * adjustment);
const txResult = await stargateClient.signAndBroadcast(
walletAddress,
[proposalMsg],
makeFeeObject({ gas }),
);

console.log(txResult)
}

await installBundle();
File renamed without changes.
File renamed without changes.
File renamed without changes.
164 changes: 0 additions & 164 deletions agoric/contract/src/kreadV1/README.md

This file was deleted.

34 changes: 0 additions & 34 deletions agoric/contract/src/kreadV2/errors.js

This file was deleted.

Loading

0 comments on commit 56f72c4

Please sign in to comment.