Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sunriselayer/sunrise into concentrated
Browse files Browse the repository at this point in the history
  • Loading branch information
jununifi committed May 27, 2024
2 parents 8193975 + 811d7bb commit 3cc62ab
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 55 deletions.
18 changes: 3 additions & 15 deletions app/abci_prepare_proposal.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"encoding/binary"
"time"

"github.com/sunriselayer/sunrise/app/ante"
Expand Down Expand Up @@ -109,22 +108,11 @@ func (app *App) PrepareProposal(req *abci.RequestPrepareProposal) (*abci.Respons
panic(err)
}

txs = AppendInfoInTxs(txs, dah.Hash(), uint64(dataSquare.Size()))

// tendermint doesn't need to use any of the erasure data, as only the
// protobuf encoded version of the block data is gossiped.
return &abci.ResponsePrepareProposal{
Txs: txs,
Txs: txs,
DataHash: dah.Hash(),
SquareSize: uint64(dataSquare.Size()),
}, nil
}

func AppendInfoInTxs(txs [][]byte, dataHash []byte, squareSize uint64) [][]byte {
// add empty byte slice
txs = append(txs, []byte{})
txs = append(txs, dataHash)
squareSizeBigEndican := make([]byte, 8)
binary.BigEndian.PutUint64(squareSizeBigEndican, squareSize)
txs = append(txs, squareSizeBigEndican)

return txs
}
31 changes: 6 additions & 25 deletions app/abci_process_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"bytes"
"encoding/binary"
"fmt"
"time"

Expand All @@ -11,6 +10,7 @@ import (
"github.com/sunriselayer/sunrise/pkg/da"
"github.com/sunriselayer/sunrise/pkg/shares"
"github.com/sunriselayer/sunrise/pkg/square"

blobtypes "github.com/sunriselayer/sunrise/x/blob/types"

"cosmossdk.io/log"
Expand Down Expand Up @@ -61,11 +61,7 @@ func (app *App) ProcessProposal(req *abci.RequestProcessProposal) (retResp *abci
return accept()
}

txs, dataHash, squareSize, err := ExtractInfoFromTxs(req.Txs)
if err != nil {
logInvalidPropBlock(app.Logger(), req.ProposerAddress, err.Error())
return reject(err)
}
txs := req.Txs

// iterate over all txs and ensure that all blobTxs are valid, PFBs are correctly signed and non
// blobTxs have no PFBs present
Expand Down Expand Up @@ -127,7 +123,6 @@ func (app *App) ProcessProposal(req *abci.RequestProcessProposal) (retResp *abci
logInvalidPropBlockError(app.Logger(), req.ProposerAddress, "invalid PFB signature", err)
return reject(err)
}

}

// Construct the data square from the block's transactions
Expand All @@ -138,8 +133,8 @@ func (app *App) ProcessProposal(req *abci.RequestProcessProposal) (retResp *abci
}

// Assert that the square size stated by the proposer is correct
if uint64(dataSquare.Size()) != squareSize {
err := fmt.Errorf("proposed square size differs from calculated square size, expected %d, got %d", squareSize, dataSquare.Size())
if uint64(dataSquare.Size()) != req.SquareSize {
err := fmt.Errorf("proposed square size differs from calculated square size, expected %d, got %d", req.SquareSize, dataSquare.Size())
logInvalidPropBlock(app.Logger(), req.ProposerAddress, err.Error())
return reject(err)
}
Expand All @@ -158,8 +153,8 @@ func (app *App) ProcessProposal(req *abci.RequestProcessProposal) (retResp *abci
// by comparing the hashes we know the computed IndexWrappers (with the share indexes of the PFB's blobs)
// are identical and that square layout is consistent. This also means that the share commitment rules
// have been followed and thus each blobs share commitment should be valid
if !bytes.Equal(dah.Hash(), dataHash) {
err := fmt.Errorf("proposed data root %X differs from calculated data root %X", dataHash, dah.Hash())
if !bytes.Equal(dah.Hash(), req.DataHash) {
err := fmt.Errorf("proposed data root %X differs from calculated data root %X", req.DataHash, dah.Hash())
logInvalidPropBlock(app.Logger(), req.ProposerAddress, err.Error())
return reject(err)
}
Expand Down Expand Up @@ -209,17 +204,3 @@ func accept() (*abci.ResponseProcessProposal, error) {
Status: abci.ResponseProcessProposal_ACCEPT,
}, nil
}

func ExtractInfoFromTxs(txsWithInfo [][]byte) (txs [][]byte, dataHash []byte, squareSize uint64, err error) {
length := len(txsWithInfo)
txs = txsWithInfo
if length >= 3 {
if len(txsWithInfo[length-3]) == 0 {
txs = txsWithInfo[:length-3]
dataHash = txsWithInfo[length-2]
squareSizeBigEndian := txsWithInfo[length-1]
squareSize = binary.BigEndian.Uint64(squareSizeBigEndian)
}
}
return
}
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,16 @@ func New(
//
// func() runtime.ValidatorAddressCodec { return <- custom validator address codec type -> }
// func() runtime.ConsensusAddressCodec { return <- custom consensus address codec type -> }

),
depinject.Provide(
//
// MINT
//

// For providing a custom inflation function for x/mint add here your
// custom function that implements the minttypes.InflationCalculationFn
// interface.
ProvideInflationCalculatorFn,
),
)
)
Expand Down
101 changes: 101 additions & 0 deletions app/inflation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package app

import (
"context"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)

var _ minttypes.InflationCalculationFn = InflationCalculationFn

const (
nanosecondsPerSecond = 1_000_000_000
secondsPerMinute = 60
minutesPerHour = 60
hoursPerDay = 24
// daysPerYear is the mean length of the Gregorian calendar year. Note this
// value isn't 365 because 97 out of 400 years are leap years. See
// https://en.wikipedia.org/wiki/Year
daysPerYear = 365.2425
secondsPerYear = int64(secondsPerMinute * minutesPerHour * hoursPerDay * daysPerYear) // 31,556,952
nanosecondsPerYear = nanosecondsPerSecond * secondsPerYear // 31,556,952,000,000,000

// genesis time - Tuesday, May 7, 2024 2:42:49 AM UTC
genesisTime = int64(1715049769)
)

var (
// initialInflationRate is the inflation rate that the network starts at.
initialInflationRate = sdkmath.LegacyMustNewDecFromStr("0.08")
// initialInflationRate is the max inflation rate at the first year based on bondedRatio
initialInflationRateMax = sdkmath.LegacyMustNewDecFromStr("0.10")
// initialInflationRate is the min inflation rate at the first year based on bondedRatio
initialInflationRateMin = sdkmath.LegacyMustNewDecFromStr("0.06")

// disinflationRate is the rate at which the inflation rate decreases each year.
disinflationRate = sdkmath.LegacyMustNewDecFromStr("0.08")
// targetInflationRate is the inflation rate that the network aims to
// stabilize at. In practice, targetInflationRate acts as a minimum so that
// the inflation rate doesn't decrease after reaching it.
targetInflationRate = sdkmath.LegacyMustNewDecFromStr("0.02")
)

func InitialInflationRate() sdkmath.LegacyDec {
return initialInflationRate
}

func InitialInflationRateMax() sdkmath.LegacyDec {
return initialInflationRateMax
}

func InitialInflationRateMin() sdkmath.LegacyDec {
return initialInflationRateMin
}

func DisinflationRate() sdkmath.LegacyDec {
return disinflationRate
}

func TargetInflationRate() sdkmath.LegacyDec {
return targetInflationRate
}

func InflationCalculationFn(ctx context.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdkmath.LegacyDec) sdkmath.LegacyDec {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return CalculateInflationRate(sdkCtx, time.Unix(genesisTime, 0), bondedRatio)
}

func ProvideInflationCalculatorFn() minttypes.InflationCalculationFn {
return InflationCalculationFn
}

// CalculateInflationRate returns the inflation rate for the current year depending on
// the current block height in context. The inflation rate is expected to
// decrease every year according to the schedule specified in the README.
func CalculateInflationRate(ctx sdk.Context, genesis time.Time, bondedRatio sdkmath.LegacyDec) sdkmath.LegacyDec {
// initialRate = initialMax - (initialMax-initialMin)*bondedRatio
initialRate := initialInflationRateMax.Sub(
initialInflationRateMax.Sub(initialInflationRateMin).Mul(bondedRatio),
)

// disinflatedRate = initialRate * (1 - disinflationRate)^((now - genesis).convertToYears())
years := yearsSinceGenesis(genesis, ctx.BlockTime())
disinflatedRate := initialRate.Mul(
sdkmath.LegacyOneDec().Sub(disinflationRate).Power(uint64(years)),
)

// finalRate = max(disinflatedRate, convergenceRate)
return sdkmath.LegacyMaxDec(disinflatedRate, TargetInflationRate())
}

// yearsSinceGenesis returns the number of years that have passed between
// genesis and current (rounded down).
func yearsSinceGenesis(genesis time.Time, current time.Time) (years int64) {
if current.Before(genesis) {
return 0
}
return current.Sub(genesis).Nanoseconds() / nanosecondsPerYear
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.21.5

replace (
// sunrise-core
github.com/cometbft/cometbft => github.com/sunriselayer/sunrise-core v0.0.3-cmt-v0.38.2
github.com/cometbft/cometbft => github.com/sunriselayer/sunrise-core v0.0.5-cmt-v0.38.2
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

github.com/sunriselayer/sunrise/pkg/blob => ./pkg/blob
Expand Down Expand Up @@ -60,8 +60,8 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.9.0
github.com/sunriselayer/sunrise/pkg/blob v0.0.0-20240418170053-30d0f121cf39
github.com/sunriselayer/sunrise/pkg/namespace v0.0.0-20240418170053-30d0f121cf39
github.com/sunriselayer/sunrise/pkg/blob v0.0.3
github.com/sunriselayer/sunrise/pkg/namespace v0.0.3
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/sync v0.7.0
golang.org/x/tools v0.20.0
Expand Down Expand Up @@ -260,8 +260,6 @@ require (
github.com/spf13/afero v1.10.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/sunrise-zone/sunrise-app/pkg/blob v0.0.0-20240106110541-17a39acb5414 // indirect
github.com/sunrise-zone/sunrise-app/pkg/namespace v0.0.0-20240106114425-fcbe9cdfd972 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1181,12 +1181,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/sunrise-zone/sunrise-app/pkg/blob v0.0.0-20240106110541-17a39acb5414 h1:9L0sdWJKKNBW5HuIlGV7kn6cVklR7fQujUjl4qPBfWA=
github.com/sunrise-zone/sunrise-app/pkg/blob v0.0.0-20240106110541-17a39acb5414/go.mod h1:lKWVDRG4sv50z+TJycD8+Vf8b3AmWVwiNoLWxJlbte8=
github.com/sunrise-zone/sunrise-app/pkg/namespace v0.0.0-20240106114425-fcbe9cdfd972 h1:G/7AApJQ6KU+DEqQ0y1BQDcLySj21RoSg/sRqtodaB8=
github.com/sunrise-zone/sunrise-app/pkg/namespace v0.0.0-20240106114425-fcbe9cdfd972/go.mod h1:u36JI/3vny+qvLQqKS2wTZeK9xQ55ijkmlcp6YodBtA=
github.com/sunriselayer/sunrise-core v0.0.3-cmt-v0.38.2 h1:VZfy+CILNSkMzVwiryECeQBWx0c5xh/nrPT1cFYcWdw=
github.com/sunriselayer/sunrise-core v0.0.3-cmt-v0.38.2/go.mod h1:y+21SZ2TVD1qS7lvEXfDKI15hEryT9sNvJ+t3LyAFi0=
github.com/sunriselayer/sunrise-core v0.0.4-cmt-v0.38.2 h1:bdUfXclzXnW+YMYffBlDHzh8gnOq4CL53qCBePRONvU=
github.com/sunriselayer/sunrise-core v0.0.4-cmt-v0.38.2/go.mod h1:oAucCZwYjgQcT/YDtHkOhG3joqsiq91eDjzQmcW2d4s=
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
Expand Down
7 changes: 7 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,13 @@ github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/handy v0.0.0-20200128134331-0f66f006fb2e h1:mOtuXaRAbVZsxAHVdPR3IjfmN8T1h2iczJLynhLybf8=
github.com/streadway/handy v0.0.0-20200128134331-0f66f006fb2e/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/sunriselayer/sunrise-core v0.0.5-cmt-v0.38.2 h1:wTUAPc8L3Trw/2cbDce5anled3qW9KIIYNVi/w/rFTU=
github.com/sunriselayer/sunrise-core v0.0.5-cmt-v0.38.2/go.mod h1:oAucCZwYjgQcT/YDtHkOhG3joqsiq91eDjzQmcW2d4s=
github.com/sunriselayer/sunrise-core v0.38.3-0.20240418164630-bac1c330887b h1:CJffEY8VCI8WhfIXhIkJ6TdcN8/xWocJM5iryq3VSIY=
github.com/sunriselayer/sunrise-core v0.38.3-0.20240418164630-bac1c330887b/go.mod h1:y+21SZ2TVD1qS7lvEXfDKI15hEryT9sNvJ+t3LyAFi0=
github.com/sunriselayer/sunrise-core v1.0.0-alpha.1 h1:9F3638Gz+e9natkL0iGV2w21uPVsTI+btDCEE0CHN6M=
github.com/sunriselayer/sunrise-core v1.0.0-alpha.1/go.mod h1:fwVpJigzDw2UnFchb0fIq7svrLmHcn5AfpMzob/xquI=
github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c h1:+aPplBwWcHBo6q9xrfWdMrT9o4kltkmmvpemgIjep/8=
github.com/t-yuki/gocover-cobertura v0.0.0-20180217150009-aaee18c8195c/go.mod h1:SbErYREK7xXdsRiigaQiQkI9McGRzYMvlKYaP3Nimdk=
github.com/tdakkota/asciicheck v0.2.0 h1:o8jvnUANo0qXtnslk2d3nMKTFNlOnJjRrNcj0j9qkHM=
Expand Down
6 changes: 3 additions & 3 deletions test/util/malicious/out_of_order_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (a *App) OutOfOrderPrepareProposal(req abci.RequestPrepareProposal) (*abci.
panic(err)
}

txs = app.AppendInfoInTxs(txs, dah.Hash(), uint64(dataSquare.Size()))

// tendermint doesn't need to use any of the erasure data, as only the
// protobuf encoded version of the block data is gossiped.
return &abci.ResponsePrepareProposal{
Txs: txs,
Txs: txs,
SquareSize: uint64(dataSquare.Size()),
DataHash: dah.Hash(),
}, nil
}

0 comments on commit 3cc62ab

Please sign in to comment.