Commit 68f886ec authored by Karl Floersch's avatar Karl Floersch

Use ring buffer for chain contracts

parent f6c20be2
...@@ -8,6 +8,7 @@ import { iOVM_BaseChain } from "../../iOVM/chain/iOVM_BaseChain.sol"; ...@@ -8,6 +8,7 @@ import { iOVM_BaseChain } from "../../iOVM/chain/iOVM_BaseChain.sol";
/* Library Imports */ /* Library Imports */
import { Lib_OVMCodec } from "../../libraries/codec/Lib_OVMCodec.sol"; import { Lib_OVMCodec } from "../../libraries/codec/Lib_OVMCodec.sol";
import { Lib_MerkleUtils } from "../../libraries/utils/Lib_MerkleUtils.sol"; import { Lib_MerkleUtils } from "../../libraries/utils/Lib_MerkleUtils.sol";
import { TimeboundRingBuffer, Lib_TimeboundRingBuffer } from "../../libraries/utils/Lib_TimeboundRingBuffer.sol";
/** /**
* @title OVM_BaseChain * @title OVM_BaseChain
...@@ -18,11 +19,23 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -18,11 +19,23 @@ contract OVM_BaseChain is iOVM_BaseChain {
* Contract Variables: Batches * * Contract Variables: Batches *
*******************************/ *******************************/
bytes32[] internal batches; using Lib_TimeboundRingBuffer for TimeboundRingBuffer;
TimeboundRingBuffer internal batches;
uint256 internal totalBatches; uint256 internal totalBatches;
uint256 internal totalElements; uint256 internal totalElements;
/***************
* Constructor *
***************/
constructor()
{
// TODO: Add propper customization
batches.init(4, 2, 100000);
}
/************************************* /*************************************
* Public Functions: Batch Retrieval * * Public Functions: Batch Retrieval *
*************************************/ *************************************/
...@@ -39,7 +52,7 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -39,7 +52,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
uint256 _totalElements uint256 _totalElements
) )
{ {
return totalElements; return uint256(uint224(batches.getExtraData()));
} }
/** /**
...@@ -54,7 +67,7 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -54,7 +67,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
uint256 _totalBatches uint256 _totalBatches
) )
{ {
return totalBatches; return uint256(batches.getLength());
} }
...@@ -82,7 +95,7 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -82,7 +95,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
) )
{ {
require( require(
_hashBatchHeader(_batchHeader) == batches[_batchHeader.batchIndex], _hashBatchHeader(_batchHeader) == batches.get(uint32(_batchHeader.batchIndex)),
"Invalid batch header." "Invalid batch header."
); );
...@@ -114,9 +127,7 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -114,9 +127,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal internal
{ {
bytes32 batchHeaderHash = _hashBatchHeader(_batchHeader); bytes32 batchHeaderHash = _hashBatchHeader(_batchHeader);
batches.push(batchHeaderHash); batches.push(batchHeaderHash, bytes28(uint224(getTotalElements() + _batchHeader.batchSize)));
totalBatches += 1;
totalElements += _batchHeader.batchSize;
} }
/** /**
...@@ -131,7 +142,7 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -131,7 +142,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal internal
{ {
Lib_OVMCodec.ChainBatchHeader memory batchHeader = Lib_OVMCodec.ChainBatchHeader({ Lib_OVMCodec.ChainBatchHeader memory batchHeader = Lib_OVMCodec.ChainBatchHeader({
batchIndex: batches.length, batchIndex: uint(batches.getLength()),
batchRoot: Lib_MerkleUtils.getMerkleRoot(_elements), batchRoot: Lib_MerkleUtils.getMerkleRoot(_elements),
batchSize: _elements.length, batchSize: _elements.length,
prevTotalElements: totalElements, prevTotalElements: totalElements,
...@@ -166,17 +177,17 @@ contract OVM_BaseChain is iOVM_BaseChain { ...@@ -166,17 +177,17 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal internal
{ {
require( require(
_batchHeader.batchIndex < batches.length, _batchHeader.batchIndex < batches.getLength(),
"Invalid batch index." "Invalid batch index."
); );
require( require(
_hashBatchHeader(_batchHeader) == batches[_batchHeader.batchIndex], _hashBatchHeader(_batchHeader) == batches.get(uint32(_batchHeader.batchIndex)),
"Invalid batch header." "Invalid batch header."
); );
totalBatches = _batchHeader.batchIndex;
totalElements = _batchHeader.prevTotalElements; totalElements = _batchHeader.prevTotalElements;
batches.deleteElementsAfter(uint32(_batchHeader.batchIndex - 1), bytes28(uint224(totalElements)));
} }
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
pragma solidity ^0.7.0; pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
/* Logging */
import { console } from "@nomiclabs/buidler/console.sol";
/* Library Imports */ /* Library Imports */
import { TimeboundRingBuffer, Lib_TimeboundRingBuffer } from "../../optimistic-ethereum/libraries/utils/Lib_TimeboundRingBuffer.sol"; import { TimeboundRingBuffer, Lib_TimeboundRingBuffer } from "../../optimistic-ethereum/libraries/utils/Lib_TimeboundRingBuffer.sol";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment