Skip to content

Commit e6d573f

Browse files
authored
Merge pull request #152 from getamis/feature/reorg_policy
casper reorg policy
2 parents 3fa3fb4 + 23e9b43 commit e6d573f

File tree

14 files changed

+1661
-13
lines changed

14 files changed

+1661
-13
lines changed

accounts/abi/bind/bind.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ func bindUnnestedTypeGo(stringKind string) (int, string) {
232232
case strings.HasPrefix(stringKind, "string"):
233233
return len("string"), "string"
234234

235+
case strings.HasPrefix(stringKind, "decimal"):
236+
parts := regexp.MustCompile(`(u)?decimal([0-9]*)`).FindStringSubmatch(stringKind)
237+
return len(parts[0]), "float64"
238+
235239
default:
236240
return len(stringKind), stringKind
237241
}
@@ -385,7 +389,8 @@ var methodNormalizer = map[Lang]func(string) string{
385389
LangJava: decapitalise,
386390
}
387391

388-
// capitalise makes a camel-case string which starts with an upper case character.
392+
// capitalise makes a camel-case string which starts with an upper case character, also removing any
393+
// prefixing underscores from the variable names.
389394
func capitalise(input string) string {
390395
for len(input) > 0 && input[0] == '_' {
391396
input = input[1:]
@@ -396,7 +401,8 @@ func capitalise(input string) string {
396401
return toCamelCase(strings.ToUpper(input[:1]) + input[1:])
397402
}
398403

399-
// decapitalise makes a camel-case string which starts with a lower case character.
404+
// decapitalise makes a camel-case string which starts with a lower case character, also removing any
405+
// prefixing underscores from the variable names.
400406
func decapitalise(input string) string {
401407
for len(input) > 0 && input[0] == '_' {
402408
input = input[1:]

accounts/abi/numbers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
int16_t = reflect.TypeOf(int16(0))
3737
int32_t = reflect.TypeOf(int32(0))
3838
int64_t = reflect.TypeOf(int64(0))
39+
float64_t = reflect.TypeOf(float64(0))
3940
address_t = reflect.TypeOf(common.Address{})
4041
int_ts = reflect.TypeOf([]int(nil))
4142
int8_ts = reflect.TypeOf([]int8(nil))

accounts/abi/pack.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package abi
1818

1919
import (
20+
"encoding/binary"
21+
"math"
2022
"math/big"
2123
"reflect"
2224

2325
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/common/math"
26+
gethMath "github.com/ethereum/go-ethereum/common/math"
2527
)
2628

2729
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
@@ -39,6 +41,17 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
3941
return packNum(reflectValue)
4042
case StringTy:
4143
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len())
44+
case DecimalTy:
45+
// https://github.com/ethereum/EIPs/issues/598
46+
// https://github.com/ethereum/pyethereum/commit/237df71d1eafeafaafd6bca16d0ba066f3d1fff3
47+
value := reflectValue.Float() * math.Pow10(t.Size)
48+
if value >= math.Exp2(256) {
49+
value = value - math.Exp2(256)
50+
}
51+
52+
result := make([]byte, 32)
53+
binary.BigEndian.PutUint32(result, uint32(value))
54+
return result
4255
case AddressTy:
4356
if reflectValue.Kind() == reflect.Array {
4457
reflectValue = mustArrayToByteSlice(reflectValue)
@@ -47,9 +60,9 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
4760
return common.LeftPadBytes(reflectValue.Bytes(), 32)
4861
case BoolTy:
4962
if reflectValue.Bool() {
50-
return math.PaddedBigBytes(common.Big1, 32)
63+
return gethMath.PaddedBigBytes(common.Big1, 32)
5164
}
52-
return math.PaddedBigBytes(common.Big0, 32)
65+
return gethMath.PaddedBigBytes(common.Big0, 32)
5366
case BytesTy:
5467
if reflectValue.Kind() == reflect.Array {
5568
reflectValue = mustArrayToByteSlice(reflectValue)

accounts/abi/type.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
HashTy
3939
FixedPointTy
4040
FunctionTy
41+
DecimalTy
4142
)
4243

4344
// Type is the reflection of the supported argument type
@@ -158,8 +159,14 @@ func NewType(t string) (typ Type, err error) {
158159
typ.T = FunctionTy
159160
typ.Size = 24
160161
typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
162+
case "decimal":
163+
// https://github.com/ethereum/vyper/blob/master/docs/types.rst
164+
typ.Size = varSize
165+
typ.Kind = reflect.Float64
166+
typ.Type = float64_t
167+
typ.T = DecimalTy
161168
default:
162-
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
169+
return Type{}, fmt.Errorf("unsupported arg type: %s type %s", t, varType)
163170
}
164171

165172
return

accounts/abi/type_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func TestTypeRegexp(t *testing.T) {
8787
{"address", Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}},
8888
{"address[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}},
8989
{"address[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}},
90+
{"decimal10", Type{Kind: reflect.Float64, Type: float64_t, Size: 10, T: DecimalTy, stringKind: "decimal10"}},
91+
{"decimal10[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]float64{}), Elem: &Type{Kind: reflect.Float64, Type: float64_t, Size: 10, T: DecimalTy, stringKind: "decimal10"}, stringKind: "decimal10[]"}},
92+
{"decimal10[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]float64{}), Elem: &Type{Kind: reflect.Float64, Type: float64_t, Size: 10, T: DecimalTy, stringKind: "decimal10"}, stringKind: "decimal10[2]"}},
9093
// TODO when fixed types are implemented properly
9194
// {"fixed", Type{}},
9295
// {"fixed128x128", Type{}},
@@ -252,6 +255,7 @@ func TestTypeCheck(t *testing.T) {
252255
{"bytes20", common.Address{}, ""},
253256
{"address", [20]byte{}, ""},
254257
{"address", common.Address{}, ""},
258+
{"decimal10", float64(1), ""},
255259
} {
256260
typ, err := NewType(test.typ)
257261
if err != nil && len(test.err) == 0 {

accounts/abi/unpack.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package abi
1919
import (
2020
"encoding/binary"
2121
"fmt"
22+
"math"
2223
"math/big"
2324
"reflect"
2425

@@ -49,6 +50,18 @@ func readInteger(kind reflect.Kind, b []byte) interface{} {
4950
}
5051
}
5152

53+
// reads the decimal
54+
// https://github.com/ethereum/EIPs/issues/598
55+
// https://github.com/ethereum/pyethereum/commit/237df71d1eafeafaafd6bca16d0ba066f3d1fff3
56+
func readDecimal(t Type, word []byte) interface{} {
57+
value := float64(binary.BigEndian.Uint32(word))
58+
if value > math.Exp2(255) {
59+
value = value - math.Exp2(256)
60+
}
61+
62+
return value / math.Pow10(t.Size)
63+
}
64+
5265
// reads a bool
5366
func readBool(word []byte) (bool, error) {
5467
for _, b := range word[:31] {
@@ -182,6 +195,8 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
182195
return readInteger(t.Kind, returnOutput), nil
183196
case BoolTy:
184197
return readBool(returnOutput)
198+
case DecimalTy:
199+
return readFixedBytes(t, returnOutput)
185200
case AddressTy:
186201
return common.BytesToAddress(returnOutput), nil
187202
case HashTy:

contracts/casper/casper.abi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"name": "__init__", "outputs": [], "inputs": [{"type": "int128", "name": "_epoch_length"}, {"type": "int128", "name": "_withdrawal_delay"}, {"type": "address", "name": "_owner"}, {"type": "address", "name": "_sighasher"}, {"type": "address", "name": "_purity_checker"}, {"type": "decimal10", "name": "_base_interest_factor"}, {"type": "decimal10", "name": "_base_penalty_factor"}, {"type": "int128", "name": "_min_deposit_size"}], "constant": false, "payable": false, "type": "constructor"}, {"name": "get_main_hash_voted_frac", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_deposit_size", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "int128", "name": "validator_index"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_total_curdyn_deposits", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_total_prevdyn_deposits", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_recommended_source_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_recommended_target_hash", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "initialize_epoch", "outputs": [], "inputs": [{"type": "int128", "name": "epoch"}], "constant": false, "payable": false, "type": "function"}, {"name": "deposit", "outputs": [], "inputs": [{"type": "address", "name": "validation_addr"}, {"type": "address", "name": "withdrawal_addr"}], "constant": false, "payable": true, "type": "function"}, {"name": "logout", "outputs": [], "inputs": [{"type": "bytes", "name": "logout_msg"}], "constant": false, "payable": false, "type": "function"}, {"name": "withdraw", "outputs": [], "inputs": [{"type": "int128", "name": "validator_index"}], "constant": false, "payable": false, "type": "function"}, {"name": "vote", "outputs": [], "inputs": [{"type": "bytes", "name": "vote_msg"}], "constant": false, "payable": false, "type": "function"}, {"name": "slash", "outputs": [], "inputs": [{"type": "bytes", "name": "vote_msg_1"}, {"type": "bytes", "name": "vote_msg_2"}], "constant": false, "payable": false, "type": "function"}, {"name": "owner_withdraw", "outputs": [], "inputs": [], "constant": false, "payable": false, "type": "function"}, {"name": "change_owner", "outputs": [], "inputs": [{"type": "address", "name": "new_owner"}], "constant": false, "payable": false, "type": "function"}, {"name": "get_validators__deposit", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_validators__start_dynasty", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_validators__end_dynasty", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_validators__addr", "outputs": [{"type": "address", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_validators__withdrawal_addr", "outputs": [{"type": "address", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_checkpoint_hashes", "outputs": [{"type": "bytes32", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_nextValidatorIndex", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_validator_indexes", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "address", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_dynasty", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_next_dynasty_wei_delta", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_second_next_dynasty_wei_delta", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_dynasty_start_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_dynasty_in_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_votes__cur_dyn_votes", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}, {"type": "int128", "name": "arg1"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_votes__prev_dyn_votes", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}, {"type": "int128", "name": "arg1"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_votes__vote_bitmap", "outputs": [{"type": "uint256", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}, {"type": "bytes32", "name": "arg1"}, {"type": "int128", "name": "arg2"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_votes__is_justified", "outputs": [{"type": "bool", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_votes__is_finalized", "outputs": [{"type": "bool", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_main_hash_justified", "outputs": [{"type": "bool", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_deposit_scale_factor", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [{"type": "int128", "name": "arg0"}], "constant": true, "payable": false, "type": "function"}, {"name": "get_last_nonvoter_rescale", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_last_voter_rescale", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_epoch_length", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_withdrawal_delay", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_current_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_last_finalized_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_last_justified_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_expected_source_epoch", "outputs": [{"type": "int128", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_reward_factor", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_base_interest_factor", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}, {"name": "get_base_penalty_factor", "outputs": [{"type": "decimal10", "name": "out"}], "inputs": [], "constant": true, "payable": false, "type": "function"}]

contracts/casper/casper.bin

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)