Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel opening should be provable and confirmed on auxiliary #448

Merged
merged 33 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7ab07e9
Renamed MetaBlockGatewayInterface to KernelGatewayInterface
Oct 31, 2018
e9dc088
Happy case
Nov 2, 2018
828dff4
Added tests
Nov 5, 2018
abdce8e
Deleted unused files
Nov 5, 2018
d8601e6
Removed kernel gateway address fromm the polling place constructor
Nov 5, 2018
981248f
Added getMetaBlockHash function
Nov 5, 2018
94ec37e
Merge remote-tracking branch 'upstream/develop' into gh_407
Nov 5, 2018
7544f45
Removed kernel related logic from auxiliary block store and moved it …
Nov 6, 2018
16fd75b
Updated tests for kernel gateway
Nov 6, 2018
d206448
Merge remote-tracking branch 'upstream/develop' into gh_407
Nov 6, 2018
0ad07a2
Fixed test cases
Nov 6, 2018
ea8a0c7
Renamed test files
Nov 6, 2018
17e24ee
Few PR feedbacks
Nov 6, 2018
caed0d2
Removed unused code
Nov 7, 2018
40084b8
Added test for validate vote for kernel changes and fixed few compila…
Nov 7, 2018
945ab5a
renamed function initializeKernelGateway to initialize
Nov 12, 2018
0249e99
Added PR review comment changes
Nov 13, 2018
a801343
Updated test case
Nov 13, 2018
e781554
PR review feedback changes
Nov 13, 2018
e053df0
Removed updateKernel function
Nov 13, 2018
e9e7656
Removed repeated code
Nov 13, 2018
24184c6
Added comments and documentation in mock contracts
Nov 13, 2018
dd1f7c9
Fixed typo
Nov 13, 2018
b1ccd3b
PR review changes
Nov 14, 2018
b471082
Using test data
Nov 14, 2018
ef4bd9c
Renamed function getMetaBlockHash to currentMetaBlockHash
Nov 15, 2018
7e4452b
Added AuxiliaryTransitionObjectInterface.sol and OriginTransitionObje…
Nov 15, 2018
45396ac
Removed extra test code
Nov 15, 2018
a79e6a2
Renamed function updateMetaBlockHeight to updateMetaBlock
Nov 15, 2018
bd29a0e
Renamed MockKernelGateway to TestKernelGateway
Nov 15, 2018
ea3a336
Contracts now include interfaces
Nov 15, 2018
6cbca6d
Removed the onlyPollingPlace comment
Nov 15, 2018
e4b3269
Updated comment for justify function
Nov 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 111 additions & 5 deletions contracts/core/AuxiliaryBlockStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import "../lib/Block.sol";
import "../lib/MetaBlock.sol";
import "../lib/SafeMath.sol";
import "./BlockStore.sol";

import "./PollingPlaceInterface.sol";
import "./KernelGatewayInterface.sol";
/**
* @title The auxiliary block store stores observations about the auxiliary
* chain.
Expand Down Expand Up @@ -52,12 +53,30 @@ contract AuxiliaryBlockStore is BlockStore {
*/
mapping(bytes32 => bytes32) public originBlockHashes;

/** The hash of the currently active kernel. */
bytes32 public kernelHash;

/** The corresponding origin block store that tracks the origin chain. */
BlockStore public originBlockStore;

/**
* The meta-block gate kernel gateway is the only contract that is allowed
* to inform opening of new kernel gateway.
*/
KernelGatewayInterface public kernelGateway;

/* Modifiers */

/**
* @notice Functions with this modifier can only be called from the address
* that is registered as the kernel gateway.
*/
modifier onlyKernelGateway() {
require(
msg.sender == address(kernelGateway),
"This method must be called from the registered kernel gateway."
);
_;
}

/* Constructor */

/**
Expand Down Expand Up @@ -125,6 +144,27 @@ contract AuxiliaryBlockStore is BlockStore {

/* External Functions */

/**
* @notice Set kernel gateway
*
* @param _kernelGateway The kernel gateway address
*/
function initializeKernelGateway(address _kernelGateway)
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
external
{
require(
_kernelGateway != address(0),
"Kernel gateway address must not be zero."
);

require(
kernelGateway == address(0),
"Kernel gateway must not be already initialized."
);

kernelGateway = KernelGatewayInterface(_kernelGateway);
}

/**
* @notice Report a block. A reported block header is stored and can then
* be part of subsequent votes.
Expand Down Expand Up @@ -236,7 +276,7 @@ contract AuxiliaryBlockStore is BlockStore {
*
* @return transitionHash_ Hash of transition object at the checkpoint.
*/
function auxiliaryTransitionHashAtBlock(
function transitionHashAtBlock(
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
bytes32 _blockHash
)
external
Expand Down Expand Up @@ -316,7 +356,7 @@ contract AuxiliaryBlockStore is BlockStore {
{
bytes32 expectedTransitionHash = MetaBlock.hashAuxiliaryTransition(
coreIdentifier,
kernelHash,
kernelHashes[_blockHash],
checkpoints[_blockHash].dynasty,
_blockHash,
accumulatedGases[_blockHash],
Expand All @@ -328,6 +368,29 @@ contract AuxiliaryBlockStore is BlockStore {
valid_ = _transitionHash == expectedTransitionHash;
}

/**
* @notice Marks a block in the block store as justified. The source and
* the target are required to know when a block is finalised.
* Only the polling place may call this method.
*
* @dev This is an external function which can be called by polling place.
* This function call justify_ function which is internal defined in
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
* super class
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _sourceBlockHash The block hash of the source of the super-
* majority link.
* @param _targetBlockHash The block hash of the block that is justified.
*/
function justify(
bytes32 _sourceBlockHash,
bytes32 _targetBlockHash
)
external
onlyPollingPlace()
{
super.justify_(_sourceBlockHash, _targetBlockHash);
schemar marked this conversation as resolved.
Show resolved Hide resolved
updateKernel(_targetBlockHash);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
}
/* Private Functions */

/**
Expand Down Expand Up @@ -378,4 +441,47 @@ contract AuxiliaryBlockStore is BlockStore {

isAncestor_ = currentCheckpoint.blockHash == _ancestor;
}

/**
* @notice Stores the kernel observation for the dynasty number.
* It also activates already reported kernel
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _blockHash The checkpoint that shall be finalized.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*/
function updateKernel(bytes32 _blockHash)
private
{

bytes32 openKernelHash =
kernelGateway.getOpenKernelHash(currentDynasty);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

// Check if any kernel is reported for activation.
if(openKernelHash != bytes32(0)) {

address[] memory updatedValidators;
uint256[] memory updatedWeights;

(updatedValidators, updatedWeights) =
kernelGateway.getUpdatedValidators(openKernelHash);

/*
* Report polling place about new validators and new finalized
* block heights.
*/
PollingPlaceInterface(pollingPlace).updateMetaBlockHeight(
updatedValidators,
updatedWeights,
originBlockStore.getCurrentDynasty(),
currentDynasty
);

// Update the active kernel hash.
kernelGateway.activateKernel(openKernelHash);
}

// Store the kernel observations for the block hash.
kernelHashes[_blockHash] = kernelGateway.getActiveKernelHash();

}
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

}
76 changes: 47 additions & 29 deletions contracts/core/BlockStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -260,33 +260,7 @@ contract BlockStore is BlockStoreInterface {
external
onlyPollingPlace()
{
bool blockValid;
string memory reason;

(blockValid, reason) = isSourceValid(_sourceBlockHash);
require(blockValid, reason);

(blockValid, reason) = isTargetValid(
_sourceBlockHash,
_targetBlockHash
);
require(blockValid, reason);

// Finalise first as it may increase the dynasty number of target.
if (distanceInEpochs(_sourceBlockHash, _targetBlockHash) == 1) {
finalise(_sourceBlockHash);
}

Checkpoint memory checkpoint = Checkpoint(
_targetBlockHash,
_sourceBlockHash,
true,
false,
currentDynasty
);
checkpoints[_targetBlockHash] = checkpoint;

emit BlockJustified(_targetBlockHash);
justify_(_sourceBlockHash, _targetBlockHash);
}

/**
Expand Down Expand Up @@ -493,6 +467,50 @@ contract BlockStore is BlockStoreInterface {

/* Internal Functions */

/**
* @notice Marks a block in the block store as justified. The source and
* the target are required to know when a block is finalised.
* Only the polling place may call this method.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _sourceBlockHash The block hash of the source of the super-
* majority link.
* @param _targetBlockHash The block hash of the block that is justified.
*/
function justify_(
bytes32 _sourceBlockHash,
bytes32 _targetBlockHash
)
internal
{
bool blockValid;
string memory reason;

(blockValid, reason) = isSourceValid(_sourceBlockHash);
require(blockValid, reason);

(blockValid, reason) = isTargetValid(
_sourceBlockHash,
_targetBlockHash
);
require(blockValid, reason);

// Finalise first as it may increase the dynasty number of target.
if (distanceInEpochs(_sourceBlockHash, _targetBlockHash) == 1) {
finalise(_sourceBlockHash);
}

Checkpoint memory checkpoint = Checkpoint(
_targetBlockHash,
_sourceBlockHash,
true,
false,
currentDynasty
);
checkpoints[_targetBlockHash] = checkpoint;

emit BlockJustified(_targetBlockHash);
}

/**
* @notice Report a block. A reported block header is stored and can then
* be part of subsequent votes.
Expand Down Expand Up @@ -616,8 +634,6 @@ contract BlockStore is BlockStoreInterface {
isCheckpoint_ = checkpoints[_blockHash].blockHash == _blockHash;
}

/* Private Functions */

/**
* @notice Finalises the checkpoint at the given block hash. Updates the
* current head and dynasty if it is above the old head.
Expand All @@ -635,6 +651,8 @@ contract BlockStore is BlockStoreInterface {
emit BlockFinalised(_blockHash);
}

/* Private Functions */
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @notice Checks if a source block is valid. The same criteria apply for
* voting and justifying, as justifying results from voting.
Expand Down
18 changes: 18 additions & 0 deletions contracts/core/BlockStoreInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@ interface BlockStoreInterface {
external
view
returns (bool reported_);

/**
* @notice Returns auxiliary transition hash at the checkpoint defined
* at given block hash.
*
* @dev It reverts transaction if checkpoint is not defined at given
* block hash.
*
* @param _blockHash The hash of the block for which transition object
* is requested.
*
* @return transitionHash_ Hash of transition object at the checkpoint.
*/
function transitionHashAtBlock(bytes32 _blockHash)
external
view
returns(bytes32 transitionHash_);

}
29 changes: 29 additions & 0 deletions contracts/core/InitializeGatewayKernelInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.4.23;

// Copyright 2018 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/** @title The interface for opening the new kernel on auxiliary. */

interface InitializeGatewayKernelInterface {

/**
* @notice Set kernel gateway
*
* @param _kernelGateway The kernel gateway address
*/
function initializeKernelGateway(address _kernelGateway)
external;

}
Loading