Skip to content

Commit

Permalink
fix: todo
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed Jul 4, 2024
1 parent f284e89 commit 98e532c
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 105 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Changed the dispatcher to close epochs based on block numbers instead of block timestamps.
- **BREAKING**: replaced the environment variable `CARTESI_EPOCH_DURATION` with `CARTESI_EPOCH_LENGTH_IN_BLOCKS` to match the new epoch algorithm, and set its default value to 7200 (1 day worth of blocks, in average, considering one block is mined every 12 seconds).
- **BREAKING**: replaced the internal environment variable `RD_EPOCH_DURATION` with `RD_EPOCH_LENGTH_IN_BLOCKS` to match the new epoch algorithm, and also set its default value to 7200.
- **BREAKING**: changed the dispatcher to use the `CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER` environment variable instead of `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER`.
- **BREAKING**: replaced the environment variable `CARTESI_EPOCH_DURATION` with `CARTESI_EPOCH_LENGTH` to match the new epoch algorithm, and set its default value to 7200 (1 day worth of blocks, in average, considering one block is mined every 12 seconds).
- **BREAKING**: replaced the internal environment variable `RD_EPOCH_DURATION` with `RD_EPOCH_LENGTH` to match the new epoch algorithm, and also set its default value to 7200.
- Bumped Rust Version to 1.78.0

### Removed

- Removed `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER` environment variable.

## [1.4.0] 2024-04-09

### Added
Expand Down
1 change: 0 additions & 1 deletion build/compose-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ services:
CARTESI_BLOCKCHAIN_IS_LEGACY: "false"
CARTESI_BLOCKCHAIN_FINALITY_OFFSET: "1"
CARTESI_CONTRACTS_APPLICATION_ADDRESS: "0x7C54E3f7A8070a54223469965A871fB8f6f88c22"
CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER: "20"
CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x325272217ae6815b494bF38cED004c5Eb8a7CdA7"
CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0x58c93F83fb3304730C95aad2E360cdb88b782010"
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768"
Expand Down
1 change: 0 additions & 1 deletion build/compose-sepolia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ services:
CARTESI_BLOCKCHAIN_IS_LEGACY: "false"
CARTESI_BLOCKCHAIN_FINALITY_OFFSET: "1"
CARTESI_CONTRACTS_APPLICATION_ADDRESS: "0x9f12D4365806FC000D6555ACB85c5371b464E506"
CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER: "4152308"
CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x76f4dCaC0920826541EE718214EEE4be07346cEE"
CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0x5827Ec9365D3a9b27bF1dB982d258Ad234D37242"
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768"
Expand Down
21 changes: 6 additions & 15 deletions cmd/gen-devnet/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
Expand Down Expand Up @@ -44,7 +43,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error)
var depInfo DeploymentInfo

// Create the Authority/History pair
contractAddresses, _, err := createContracts(ctx,
contractAddresses, err := createContracts(ctx,
common.Address.Hex(addresses.GetTestBook().AuthorityHistoryPairFactory),
"newAuthorityHistoryPair(address,bytes32)(address,address)",
CONTRACT_OWNER_ADDRESS,
Expand All @@ -57,7 +56,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error)
depInfo.HistoryAddress = contractAddresses[1]

// Create the Application, passing the address of the newly created Authority
contractAddresses, blockNumber, err := createContracts(ctx,
contractAddresses, err = createContracts(ctx,
common.Address.Hex(addresses.GetTestBook().CartesiDAppFactory),
"newApplication(address,address,bytes32,bytes32)(address)",
depInfo.AuthorityAddress,
Expand All @@ -69,7 +68,6 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error)
}

depInfo.ApplicationAddress = contractAddresses[0]
depInfo.BlockNumber = blockNumber

return depInfo, nil
}
Expand All @@ -80,7 +78,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error)
//
// Warning: a second call to a contract with the same arguments will fail.
func createContracts(ctx context.Context,
args ...string) ([]string, string, error) {
args ...string) ([]string, error) {
commonArgs := []string{"--rpc-url", RPC_URL}
commonArgs = append(commonArgs, args...)

Expand All @@ -94,7 +92,7 @@ func createContracts(ctx context.Context,
castCall.Stdout = &outStrBuilder
err := castCall.Run()
if err != nil {
return contractAddresses, "", fmt.Errorf("command failed %v: %v", castCall.Args, err)
return contractAddresses, fmt.Errorf("command failed %v: %v", castCall.Args, err)
}
contractAddresses = strings.Fields(outStrBuilder.String())

Expand All @@ -110,20 +108,13 @@ func createContracts(ctx context.Context,
castSend.Stdout = &outStrBuilder
err = castSend.Run()
if err != nil {
return contractAddresses, "", fmt.Errorf("command failed %v: %v", castSend.Args, err)
return contractAddresses, fmt.Errorf("command failed %v: %v", castSend.Args, err)
}

if VerboseLog {
fmt.Printf("deployer: command: %s\n", castSend.Args)
fmt.Printf("deployer: output: %s\n", outStrBuilder.String())
}
// Extract blockNumber from JSON output
jsonMap := make(map[string](any))
err = json.Unmarshal([]byte([]byte(outStrBuilder.String())), &jsonMap)
if err != nil {
return contractAddresses, "", fmt.Errorf("failed to extract block number, %s", err.Error())
}
blockNumber := jsonMap["blockNumber"].(string)

return contractAddresses, blockNumber, nil
return contractAddresses, nil
}
1 change: 0 additions & 1 deletion cmd/gen-devnet/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ type DeploymentInfo struct {
AuthorityAddress string `json:"CARTESI_CONTRACTS_AUTHORITY_ADDRESS"`
HistoryAddress string `json:"CARTESI_CONTRACTS_HISTORY_ADDRESS"`
ApplicationAddress string `json:"CARTESI_CONTRACTS_APPLICATION_ADDRESS"`
BlockNumber string `json:"CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER"`
}
8 changes: 1 addition & 7 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ Address of the DApp's contract.

* **Type:** `string`

## `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER`

Block in which the DApp's contract was deployed.

* **Type:** `int64`

## `CARTESI_CONTRACTS_AUTHORITY_ADDRESS`

Address of the Authority contract.
Expand Down Expand Up @@ -236,7 +230,7 @@ for more information.
* **Type:** `string`
* **Default:** `""`

## `CARTESI_EPOCH_LENGTH_IN_BLOCKS`
## `CARTESI_EPOCH_LENGTH`

Length of a rollups epoch in blocks.

Expand Down
6 changes: 2 additions & 4 deletions internal/node/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type NodeConfig struct {
LogLevel LogLevel
LogPretty bool
RollupsEpochLengthInBlocks uint64
RollupsEpochLength uint64
BlockchainID uint64
BlockchainHttpEndpoint Redacted[string]
BlockchainWsEndpoint Redacted[string]
Expand Down Expand Up @@ -75,16 +75,14 @@ func FromEnv() NodeConfig {
var config NodeConfig
config.LogLevel = getLogLevel()
config.LogPretty = getLogPretty()
config.RollupsEpochLengthInBlocks = getEpochLengthInBlocks()
config.RollupsEpochLength = getEpochLength()
config.BlockchainID = getBlockchainId()
config.BlockchainHttpEndpoint = Redacted[string]{getBlockchainHttpEndpoint()}
config.BlockchainWsEndpoint = Redacted[string]{getBlockchainWsEndpoint()}
config.BlockchainIsLegacy = getBlockchainIsLegacy()
config.BlockchainFinalityOffset = getBlockchainFinalityOffset()
config.BlockchainBlockTimeout = getBlockchainBlockTimeout()
config.ContractsApplicationAddress = getContractsApplicationAddress()
config.ContractsApplicationDeploymentBlockNumber =
getContractsApplicationDeploymentBlockNumber()
config.ContractsHistoryAddress = getContractsHistoryAddress()
config.ContractsAuthorityAddress = getContractsAuthorityAddress()
config.ContractsInputBoxAddress = getContractsInputBoxAddress()
Expand Down
7 changes: 1 addition & 6 deletions internal/node/config/generate/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ the snapshot matches the hash in the Application contract."""
# Rollups
#

[rollups.CARTESI_EPOCH_LENGTH_IN_BLOCKS]
[rollups.CARTESI_EPOCH_LENGTH]
default = "7200" # 1 day (average) in blocks (considering one block is mined every 12 seconds)
go-type = "uint64"
description = """
Expand Down Expand Up @@ -101,11 +101,6 @@ go-type = "string"
description = """
Address of the DApp's contract."""

[contracts.CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER]
go-type = "int64"
description = """
Block in which the DApp's contract was deployed."""

[contracts.CARTESI_CONTRACTS_HISTORY_ADDRESS]
go-type = "string"
description = """
Expand Down
18 changes: 3 additions & 15 deletions internal/node/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/node/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func newDispatcher(c config.NodeConfig, workDir string) services.CommandService
s.Env = append(s.Env, fmt.Sprintf("HISTORY_ADDRESS=%v", c.ContractsHistoryAddress))
s.Env = append(s.Env, fmt.Sprintf("AUTHORITY_ADDRESS=%v", c.ContractsAuthorityAddress))
s.Env = append(s.Env, fmt.Sprintf("INPUT_BOX_ADDRESS=%v", c.ContractsInputBoxAddress))
s.Env = append(s.Env, fmt.Sprintf("RD_EPOCH_LENGTH_IN_BLOCKS=%v", c.RollupsEpochLengthInBlocks))
s.Env = append(s.Env, fmt.Sprintf("RD_EPOCH_LENGTH=%v", c.RollupsEpochLength))
s.Env = append(s.Env, fmt.Sprintf("CHAIN_ID=%v", c.BlockchainID))
s.Env = append(s.Env, fmt.Sprintf("DISPATCHER_HTTP_SERVER_PORT=%v",
getPort(c, portOffsetDispatcher)))
Expand Down
6 changes: 3 additions & 3 deletions offchain/dispatcher/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct DispatcherEnvCLIConfig {

/// Duration of rollups epoch in blocks, for which dispatcher will make claims.
#[arg(long, env, default_value = "7200")]
pub rd_epoch_length_in_blocks: u64,
pub rd_epoch_length: u64,

/// Chain ID
#[arg(long, env)]
Expand All @@ -46,7 +46,7 @@ pub struct DispatcherConfig {
pub log_config: LogConfig,
pub blockchain_config: BlockchainConfig,

pub epoch_length_in_blocks: u64,
pub epoch_length: u64,
pub chain_id: u64,
}

Expand Down Expand Up @@ -86,7 +86,7 @@ impl Config {
broker_config,
log_config,
blockchain_config,
epoch_length_in_blocks: dispatcher_config.rd_epoch_length_in_blocks,
epoch_length: dispatcher_config.rd_epoch_length,
chain_id: dispatcher_config.chain_id,
};

Expand Down
49 changes: 30 additions & 19 deletions offchain/dispatcher/src/drivers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ impl Context {
.inc();

self.inputs_sent += 1;
self.last_input_epoch = Some(self.calculate_epoch(input_block_number));

let input_epoch = self.calculate_epoch(input_block_number);
self.last_finished_epoch.map(|last_finished_epoch| {
assert!(self.last_input_epoch.unwrap() >= last_finished_epoch)
// Asserting that the calculated epoch comes after the last finished epoch.
// (If last_finished_epoch == None then we don't need the assertion.)
assert!(input_epoch > last_finished_epoch)
});
self.last_input_epoch = Some(input_epoch);

Ok(())
}
Expand Down Expand Up @@ -129,6 +133,13 @@ impl Context {
.get_or_create(&self.dapp_metadata)
.inc();

// Asserting that there are inputs in the current epoch.
assert!(self.inputs_sent > 0);
assert!(self.last_input_epoch.is_some());
self.last_finished_epoch.map(|last_finished_epoch| {
assert!(self.last_input_epoch.unwrap() > last_finished_epoch)
});

self.last_finished_epoch = self.last_input_epoch;
Ok(())
}
Expand Down Expand Up @@ -404,7 +415,7 @@ mod tests {
let result = context.finish_epoch_if_needed(12, &broker).await;
assert!(result.is_ok());
broker.assert_state(vec![
Event::Finish, //
Event::FinishEpoch(0), //
]);
}

Expand Down Expand Up @@ -457,7 +468,7 @@ mod tests {
assert_eq!(context.last_finished_epoch, Some(1));

broker.assert_state(vec![
Event::Finish,
Event::FinishEpoch(0),
Event::Input(number_of_inputs_sent),
]);
}
Expand Down Expand Up @@ -499,10 +510,10 @@ mod tests {
expected: vec![
Event::Input(0),
Event::Input(1),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(2),
Event::Input(3),
Event::Finish,
Event::FinishEpoch(1),
],
},
Case {
Expand All @@ -513,10 +524,10 @@ mod tests {
Event::Input(0),
Event::Input(1),
Event::Input(2),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(3),
Event::Input(4),
Event::Finish,
Event::FinishEpoch(1),
],
},
Case {
Expand All @@ -525,9 +536,9 @@ mod tests {
last_block: 4,
expected: vec![
Event::Input(0),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(1),
Event::Finish,
Event::FinishEpoch(1),
],
},
Case {
Expand All @@ -536,11 +547,11 @@ mod tests {
last_block: 6,
expected: vec![
Event::Input(0),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(1),
Event::Finish,
Event::FinishEpoch(1),
Event::Input(2),
Event::Finish,
Event::FinishEpoch(2),
],
},
Case {
Expand All @@ -551,15 +562,15 @@ mod tests {
Event::Input(0),
Event::Input(1),
Event::Input(2),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(3),
Event::Input(4),
Event::Finish,
Event::FinishEpoch(1),
Event::Input(5),
Event::Input(6),
Event::Input(7),
Event::Input(8),
Event::Finish,
Event::FinishEpoch(2),
Event::Input(9),
Event::Input(10),
],
Expand All @@ -570,11 +581,11 @@ mod tests {
last_block: 10,
expected: vec![
Event::Input(0),
Event::Finish,
Event::FinishEpoch(0),
Event::Input(1),
Event::Finish,
Event::FinishEpoch(1),
Event::Input(2),
Event::Finish,
Event::FinishEpoch(2),
],
},
];
Expand Down
Loading

0 comments on commit 98e532c

Please sign in to comment.