Skip to content

Commit

Permalink
feat: adding sortition interval to the parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Apr 1, 2023
1 parent a8c6be9 commit b647f74
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (exe *Execution) checkStamp(trx *tx.Tx, sb sandbox.Sandbox) error {
if trx.IsSubsidyTx() {
interval = 1
} else if trx.IsSortitionTx() {
interval = 7
interval = sb.Params().SortitionInterval
}

if !ok || curHeight-height > interval {
Expand Down
2 changes: 1 addition & 1 deletion types/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestGenesisTestNet(t *testing.T) {
assert.Equal(t, g.GenesisTime(), genTime)
assert.Equal(t, g.Params().BondInterval, uint32(120))

expected, _ := hash.FromString("b2c9af5adb7b0ff29b2ed2f16b6aa31f4aff090efdecb2bf4fb5d804a8f98351")
expected, _ := hash.FromString("e5e9479aaed9923b77c016548dd41741062d376fc1e98362b47458d49ddb5229")
assert.Equal(t, g.Hash(), expected)
}

Expand Down
10 changes: 6 additions & 4 deletions types/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ type Params struct {
TransactionToLiveInterval uint32 `cbor:"5,keyasint"`
BondInterval uint32 `cbor:"6,keyasint"`
UnbondInterval uint32 `cbor:"7,keyasint"`
FeeFraction float64 `cbor:"8,keyasint"`
MinimumFee int64 `cbor:"9,keyasint"`
MaximumFee int64 `cbor:"10,keyasint"`
MaximumStake int64 `cbor:"11,keyasint"`
SortitionInterval uint32 `cbor:"8,keyasint"`
FeeFraction float64 `cbor:"9,keyasint"`
MinimumFee int64 `cbor:"10,keyasint"`
MaximumFee int64 `cbor:"11,keyasint"`
MaximumStake int64 `cbor:"12,keyasint"`
}

func DefaultParams() Params {
Expand All @@ -25,6 +26,7 @@ func DefaultParams() Params {
TransactionToLiveInterval: 8640, // one day
BondInterval: 360, // one hour
UnbondInterval: 181440, // 21 days
SortitionInterval: 7,
FeeFraction: 0.0001,
MinimumFee: 1000,
MaximumFee: 100000000,
Expand Down
20 changes: 20 additions & 0 deletions util/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ func SafeCmp(s1, s2 []byte) bool {
return subtle.ConstantTimeCompare(s1, s2) == 1
}

// Merge accepts multiple slices and returns a single merged slice.
func Merge[T any](slices ...[]T) []T {
var totalLength int

// Calculate the total length of the merged slice.
for _, slice := range slices {
totalLength += len(slice)
}

// Create a merged slice with the appropriate capacity.
merged := make([]T, 0, totalLength)

// Append each input slice to the merged slice.
for _, slice := range slices {
merged = append(merged, slice...)
}

return merged
}

// Reverse replace the contents of a slice with the same elements but in
// reverse order.
func Reverse[S ~[]E, E any](s S) {
Expand Down
19 changes: 19 additions & 0 deletions util/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ func TestSafeCmp(t *testing.T) {
assert.False(t, SafeCmp([]byte{1, 2, 3, 3}, []byte{1, 2, 3}))
}

func TestMerge(t *testing.T) {
tests := []struct {
slices [][]byte
merged []byte
}{
{[][]byte{nil}, []byte{}},
{[][]byte{{0, 1, 2}}, []byte{0, 1, 2}},
{[][]byte{{}}, []byte{}},
{[][]byte{{}, {}}, []byte{}},
{[][]byte{{0}, {0}}, []byte{0, 0}},
{[][]byte{{0}, {1}, {2}}, []byte{0, 1, 2}},
}

for _, test := range tests {
merged := Merge(test.slices...)
assert.Equal(t, merged, test.merged)
}
}

func TestReverse(t *testing.T) {
tests := []struct {
slice []byte
Expand Down

0 comments on commit b647f74

Please sign in to comment.