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";
/* Library Imports */
import { Lib_OVMCodec } from "../../libraries/codec/Lib_OVMCodec.sol";
import { Lib_MerkleUtils } from "../../libraries/utils/Lib_MerkleUtils.sol";
import { TimeboundRingBuffer, Lib_TimeboundRingBuffer } from "../../libraries/utils/Lib_TimeboundRingBuffer.sol";
/**
* @title OVM_BaseChain
......@@ -18,11 +19,23 @@ contract OVM_BaseChain is iOVM_BaseChain {
* Contract Variables: Batches *
*******************************/
bytes32[] internal batches;
using Lib_TimeboundRingBuffer for TimeboundRingBuffer;
TimeboundRingBuffer internal batches;
uint256 internal totalBatches;
uint256 internal totalElements;
/***************
* Constructor *
***************/
constructor()
{
// TODO: Add propper customization
batches.init(4, 2, 100000);
}
/*************************************
* Public Functions: Batch Retrieval *
*************************************/
......@@ -39,7 +52,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
uint256 _totalElements
)
{
return totalElements;
return uint256(uint224(batches.getExtraData()));
}
/**
......@@ -54,7 +67,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
uint256 _totalBatches
)
{
return totalBatches;
return uint256(batches.getLength());
}
......@@ -82,7 +95,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
)
{
require(
_hashBatchHeader(_batchHeader) == batches[_batchHeader.batchIndex],
_hashBatchHeader(_batchHeader) == batches.get(uint32(_batchHeader.batchIndex)),
"Invalid batch header."
);
......@@ -114,9 +127,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal
{
bytes32 batchHeaderHash = _hashBatchHeader(_batchHeader);
batches.push(batchHeaderHash);
totalBatches += 1;
totalElements += _batchHeader.batchSize;
batches.push(batchHeaderHash, bytes28(uint224(getTotalElements() + _batchHeader.batchSize)));
}
/**
......@@ -131,7 +142,7 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal
{
Lib_OVMCodec.ChainBatchHeader memory batchHeader = Lib_OVMCodec.ChainBatchHeader({
batchIndex: batches.length,
batchIndex: uint(batches.getLength()),
batchRoot: Lib_MerkleUtils.getMerkleRoot(_elements),
batchSize: _elements.length,
prevTotalElements: totalElements,
......@@ -166,17 +177,17 @@ contract OVM_BaseChain is iOVM_BaseChain {
internal
{
require(
_batchHeader.batchIndex < batches.length,
_batchHeader.batchIndex < batches.getLength(),
"Invalid batch index."
);
require(
_hashBatchHeader(_batchHeader) == batches[_batchHeader.batchIndex],
_hashBatchHeader(_batchHeader) == batches.get(uint32(_batchHeader.batchIndex)),
"Invalid batch header."
);
totalBatches = _batchHeader.batchIndex;
totalElements = _batchHeader.prevTotalElements;
batches.deleteElementsAfter(uint32(_batchHeader.batchIndex - 1), bytes28(uint224(totalElements)));
}
......
......@@ -2,9 +2,6 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/* Logging */
import { console } from "@nomiclabs/buidler/console.sol";
/* Library Imports */
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