Skip to content

Commit

Permalink
Merge pull request #1 from eosnetworkfoundation/audit-updates
Browse files Browse the repository at this point in the history
Sanity check for initialization values and sum overflows
  • Loading branch information
nsjames authored May 22, 2024
2 parents f9af3f8 + 00bcc7c commit a9f78c3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
*.abi
*.wasm
!/external/**/*.abi
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ $ npm test

> test
> bun test
```
```

#### Exported memory errors

```
TypeError: undefined is not an object (evaluating 'this.memory.buffer')
```

If you're using a version of CDT to build that doesn't support exported memory, you'll need to export it manually for VeRT tests to work.

```bash
# Grab wabt
sudo apt-get install wabt

# Create a temporary wat file and export the memory
wasm2wat eosio.fees.wasm | sed -e 's|(memory |(memory (export "memory") |' > eosio.fees.wat
wat2wasm -o eosio.fees.wasm eosio.fees.wat
rm eosio.fees.wat
```

You can also use the `./build.sh` script that will handle building and exporting memory for you.
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

cdt-cpp eosio.fees.cpp -I ./include
wasm2wat eosio.fees.wasm | sed -e 's|(memory |(memory (export "memory") |' > eosio.fees.wat
wat2wasm -o eosio.fees.wasm eosio.fees.wat
rm eosio.fees.wat
Binary file added bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion eosio.fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace eosio {
[[eosio::action]]
void fees::init( const uint32_t epoch_time_interval ) {
require_auth( get_self() );
check( epoch_time_interval > 0, "epoch_time_interval must be greater than 0" );

settings_table _settings( get_self(), get_self().value );
auto settings = _settings.get_or_default();
Expand Down Expand Up @@ -55,7 +56,7 @@ void fees::distribute()
update_next_epoch();

strategies_table _strategies( get_self(), get_self().value );
const uint16_t total_weight = get_total_weight();
const uint32_t total_weight = get_total_weight();

// distributing fees in EOS
const asset balance = eosio::token::get_balance( "eosio.token"_n, get_self(), symbol_code("EOS") );
Expand Down
4 changes: 2 additions & 2 deletions eosio.fees.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ namespace eosio {
* @param contract - contract name
* @return uint16_t - total weight
*/
static uint16_t get_total_weight( const name contract = "eosio.fees"_n ) {
static uint32_t get_total_weight( const name contract = "eosio.fees"_n ) {
strategies_table _strategies( contract, contract.value );
uint16_t total_weight = 0;
uint32_t total_weight = 0;
for (auto& row : _strategies) {
total_weight += row.weight;
}
Expand Down
7 changes: 7 additions & 0 deletions eosio.fees.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ describe(fees_contract, () => {
)
});

test("Cannot init with a 0 interval", async () => {
await expectToThrow(
contracts.fees.actions.init([0]).send(),
"eosio_assert: epoch_time_interval must be greater than 0"
)
});

test('eosio.fees::init', async () => {
await contracts.fees.actions.init([TEN_MINUTES]).send()

Expand Down

0 comments on commit a9f78c3

Please sign in to comment.