Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update some code and fix all the unit tests #134

Merged
merged 64 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
745af25
Refactor system transactions
devfans Aug 25, 2023
0307617
Small error fix
devfans Aug 25, 2023
1954a7c
Refactor damn stuff
devfans Aug 26, 2023
6796667
Ensure system transactions will be not reverted
devfans Aug 28, 2023
1242be9
Remove some quotes
devfans Aug 30, 2023
6d2dbfb
Fix test units
devfans Sep 1, 2023
c8ebaa0
optimise code
siovanus Sep 5, 2023
eb82b11
change consensus engine restart to reset;move consensus state unlock
siovanus Sep 5, 2023
8da02e7
add p2p keystore
siovanus Sep 6, 2023
25d379e
Use seal hash as block hash for now
devfans Sep 11, 2023
826e966
Fix miner unit tests
devfans Sep 12, 2023
d5e8adb
Ignore system tx base fee check
devfans Sep 13, 2023
99cbffe
Fix most unit tests in native contracts
devfans Sep 13, 2023
4c3cc08
Add docker build
devfans Sep 13, 2023
92bcc6d
Fix makefile update
devfans Sep 13, 2023
43d60dd
Use Dockerfile.build_local for zion-local
devfans Sep 13, 2023
4f042d8
Add genesis test utils
devfans Sep 14, 2023
37d3d1e
Remove stale method from interface
devfans Sep 14, 2023
f72cf75
Merge remote-tracking branch 'stefan/enh-system-tx' into develop
devfans Sep 14, 2023
832757f
Merge remote-tracking branch 'mh/keystore' into develop
devfans Sep 14, 2023
dd4e141
Merge remote-tracking branch 'stefan/block-hash-without-seal' into de…
devfans Sep 14, 2023
8ba8903
Merge remote-tracking branch 'stefan/fix-unit-tests' into develop
devfans Sep 14, 2023
fd4d54b
Merge remote-tracking branch 'stefan/docker-build' into develop
devfans Sep 14, 2023
2b93676
Remove system gas vars
devfans Sep 14, 2023
7221b44
Merge branch 'enh-system-tx' into develop
devfans Sep 14, 2023
1f1f4d0
Fix system tx gas
devfans Sep 14, 2023
4a2a27f
Merge branch 'enh-system-tx' into develop
devfans Sep 14, 2023
828d5ad
Remove duplicate basic gas
devfans Sep 14, 2023
0095b73
Merge branch 'enh-system-tx' into develop
devfans Sep 14, 2023
0b1a384
Update header seal hash to include nonce and mix digest
devfans Sep 15, 2023
0f24c75
Remove duplicate intrinsic gas cost for native contract calls
devfans Sep 15, 2023
99bb7a0
Fix system tx hash issue
devfans Sep 15, 2023
be649a3
Merge branch 'fix-unit-tests' into develop
devfans Sep 15, 2023
c72841e
remove chainid constrain
siovanus Sep 15, 2023
3ebd713
update consensus
siovanus Sep 18, 2023
ae73cfe
Merge remote-tracking branch 'mh/optimise' into develop
devfans Sep 18, 2023
40e7873
update mock&backend test
rain-zxn Sep 20, 2023
49f84c6
Merge pull request #1 from rain-zxn/rain_dev
devfans Sep 20, 2023
34a99be
Fix unit tests in accounts
devfans Sep 20, 2023
7a3fa56
fix unit test if consensus
siovanus Sep 21, 2023
d970f7e
Fix unit tests in ethclient
devfans Sep 21, 2023
3dfa2c0
Merge from mh
devfans Sep 22, 2023
3bbf02e
Fix unit tests in eth folder
devfans Sep 22, 2023
493e3b3
fix console
siovanus Sep 23, 2023
e8e080b
Fix fast sync unit tests
devfans Sep 25, 2023
10dadde
Fix unit tests in cmd
devfans Sep 25, 2023
fb444e9
Fix unit tests in p2p
devfans Sep 26, 2023
e3250ef
Add missing lru
devfans Sep 26, 2023
2cf1c20
temp
siovanus Sep 26, 2023
d66a46a
temp
siovanus Sep 26, 2023
f2050f6
fix most uni test
siovanus Sep 27, 2023
cf6b7be
Fix merge conflicts
devfans Sep 27, 2023
ce4e0b4
Fix broken tests in accounts
devfans Sep 27, 2023
6900108
Fix broken tests in graphql
devfans Sep 27, 2023
65ee18c
Fix rest bugs
devfans Sep 27, 2023
0640234
Small fixes
devfans Sep 27, 2023
f2b0a4c
Fix TestParitySturebyConverter
devfans Sep 27, 2023
8875634
Fix genesis panic in cmd
devfans Sep 27, 2023
f2f3456
Fix genesis community rate panic in cmd
devfans Sep 27, 2023
e763a83
Fix block gas limit test
devfans Sep 28, 2023
d1f7be9
Upgrade to event.Feed in hotstuff
devfans Sep 28, 2023
bdce8b8
Fix hotstuff handle unit tests, improve hotstuff event loop
devfans Sep 28, 2023
143a3bc
Remove stale test unit
devfans Sep 28, 2023
261083a
Merge branch 'master' into fix-unit-tests-more
devfans Oct 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 10 additions & 34 deletions accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ABI struct {
Constructor Method
Methods map[string]Method
Events map[string]Event
Errors map[string]Error

// Additional "special" functions introduced in solidity v0.6.0.
// It's separated from the original default fallback. Each contract
Expand Down Expand Up @@ -86,15 +87,15 @@ func (abi ABI) getArguments(name string, data []byte) (Arguments, error) {
var args Arguments
if method, ok := abi.Methods[name]; ok {
if len(data)%32 != 0 {
return nil, fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data)
return nil, fmt.Errorf("abi: improperly formatted output: %q - Bytes: %+v", data, data)
}
args = method.Outputs
}
if event, ok := abi.Events[name]; ok {
args = event.Inputs
}
if args == nil {
return nil, errors.New("abi: could not locate named method or event")
return nil, fmt.Errorf("abi: could not locate named method or event: %s", name)
}
return args, nil
}
Expand Down Expand Up @@ -157,12 +158,13 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
}
abi.Methods = make(map[string]Method)
abi.Events = make(map[string]Event)
abi.Errors = make(map[string]Error)
for _, field := range fields {
switch field.Type {
case "constructor":
abi.Constructor = NewMethod("", "", Constructor, field.StateMutability, field.Constant, field.Payable, field.Inputs, nil)
case "function":
name := abi.overloadedMethodName(field.Name)
name := ResolveNameConflict(field.Name, func(s string) bool { _, ok := abi.Methods[s]; return ok })
abi.Methods[name] = NewMethod(name, field.Name, Function, field.StateMutability, field.Constant, field.Payable, field.Inputs, field.Outputs)
case "fallback":
// New introduced function type in v0.6.0, check more detail
Expand All @@ -182,45 +184,19 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
}
abi.Receive = NewMethod("", "", Receive, field.StateMutability, field.Constant, field.Payable, nil, nil)
case "event":
name := abi.overloadedEventName(field.Name)
name := ResolveNameConflict(field.Name, func(s string) bool { _, ok := abi.Events[s]; return ok })
abi.Events[name] = NewEvent(name, field.Name, field.Anonymous, field.Inputs)
case "error":
// Errors cannot be overloaded or overridden but are inherited,
// no need to resolve the name conflict here.
abi.Errors[field.Name] = NewError(field.Name, field.Inputs)
default:
return fmt.Errorf("abi: could not recognize type %v of field %v", field.Type, field.Name)
}
}
return nil
}

// overloadedMethodName returns the next available name for a given function.
// Needed since solidity allows for function overload.
//
// e.g. if the abi contains Methods send, send1
// overloadedMethodName would return send2 for input send.
func (abi *ABI) overloadedMethodName(rawName string) string {
name := rawName
_, ok := abi.Methods[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", rawName, idx)
_, ok = abi.Methods[name]
}
return name
}

// overloadedEventName returns the next available name for a given event.
// Needed since solidity allows for event overload.
//
// e.g. if the abi contains events received, received1
// overloadedEventName would return received2 for input received.
func (abi *ABI) overloadedEventName(rawName string) string {
name := rawName
_, ok := abi.Events[name]
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", rawName, idx)
_, ok = abi.Events[name]
}
return name
}

// MethodById looks up a method by the 4-byte id,
// returns nil if none found.
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
Expand Down
52 changes: 35 additions & 17 deletions accounts/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ func TestInvalidABI(t *testing.T) {

// TestConstructor tests a constructor function.
// The test is based on the following contract:
// contract TestConstructor {
// constructor(uint256 a, uint256 b) public{}
//
// contract TestConstructor {
// constructor(uint256 a, uint256 b) public{}
// }
func TestConstructor(t *testing.T) {
json := `[{ "inputs": [{"internalType": "uint256","name": "a","type": "uint256" },{ "internalType": "uint256","name": "b","type": "uint256"}],"stateMutability": "nonpayable","type": "constructor"}]`
Expand Down Expand Up @@ -295,6 +296,20 @@ func TestOverloadedMethodSignature(t *testing.T) {
check("bar0", "bar(uint256,uint256)", false)
}

func TestCustomErrors(t *testing.T) {
json := `[{ "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ],"name": "MyError", "type": "error"} ]`
abi, err := JSON(strings.NewReader(json))
if err != nil {
t.Fatal(err)
}
check := func(name string, expect string) {
if abi.Errors[name].Sig != expect {
t.Fatalf("The signature of overloaded method mismatch, want %s, have %s", expect, abi.Methods[name].Sig)
}
}
check("MyError", "MyError(uint256)")
}

func TestMultiPack(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata))
if err != nil {
Expand Down Expand Up @@ -710,16 +725,19 @@ func TestBareEvents(t *testing.T) {
}

// TestUnpackEvent is based on this contract:
// contract T {
// event received(address sender, uint amount, bytes memo);
// event receivedAddr(address sender);
// function receive(bytes memo) external payable {
// received(msg.sender, msg.value, memo);
// receivedAddr(msg.sender);
// }
// }
//
// contract T {
// event received(address sender, uint amount, bytes memo);
// event receivedAddr(address sender);
// function receive(bytes memo) external payable {
// received(msg.sender, msg.value, memo);
// receivedAddr(msg.sender);
// }
// }
//
// When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
// receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
//
// receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
func TestUnpackEvent(t *testing.T) {
const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
abi, err := JSON(strings.NewReader(abiJSON))
Expand Down Expand Up @@ -1024,9 +1042,7 @@ func TestABI_EventById(t *testing.T) {
}
if event == nil {
t.Errorf("We should find a event for topic %s, test #%d", topicID.Hex(), testnum)
}

if event.ID != topicID {
} else if event.ID != topicID {
t.Errorf("Event id %s does not match topic %s, test #%d", event.ID.Hex(), topicID.Hex(), testnum)
}

Expand Down Expand Up @@ -1066,8 +1082,9 @@ func TestDoubleDuplicateMethodNames(t *testing.T) {
// TestDoubleDuplicateEventNames checks that if send0 already exists, there won't be a name
// conflict and that the second send event will be renamed send1.
// The test runs the abi of the following contract.
// contract DuplicateEvent {
// event send(uint256 a);
//
// contract DuplicateEvent {
// event send(uint256 a);
// event send0();
// event send();
// }
Expand All @@ -1094,7 +1111,8 @@ func TestDoubleDuplicateEventNames(t *testing.T) {
// TestUnnamedEventParam checks that an event with unnamed parameters is
// correctly handled.
// The test runs the abi of the following contract.
// contract TestEvent {
//
// contract TestEvent {
// event send(uint256, uint256);
// }
func TestUnnamedEventParam(t *testing.T) {
Expand Down
31 changes: 13 additions & 18 deletions accounts/abi/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package abi

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -78,16 +79,10 @@ func (arguments Arguments) isTuple() bool {
// Unpack performs the operation hexdata -> Go format.
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
if len(data) == 0 {
if len(arguments) != 0 {
return nil, fmt.Errorf("abi: attempting to unmarshall an empty string while arguments are expected")
if len(arguments.NonIndexed()) != 0 {
return nil, errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
}
// Nothing to unmarshal, return default variables
nonIndexedArgs := arguments.NonIndexed()
defaultVars := make([]interface{}, len(nonIndexedArgs))
for index, arg := range nonIndexedArgs {
defaultVars[index] = reflect.New(arg.Type.GetType())
}
return defaultVars, nil
return make([]interface{}, 0), nil
}
return arguments.UnpackValues(data)
}
Expand All @@ -96,11 +91,11 @@ func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error {
// Make sure map is not nil
if v == nil {
return fmt.Errorf("abi: cannot unpack into a nil map")
return errors.New("abi: cannot unpack into a nil map")
}
if len(data) == 0 {
if len(arguments) != 0 {
return fmt.Errorf("abi: attempting to unmarshall an empty string while arguments are expected")
if len(arguments.NonIndexed()) != 0 {
return errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
}
return nil // Nothing to unmarshal, return
}
Expand All @@ -121,8 +116,8 @@ func (arguments Arguments) Copy(v interface{}, values []interface{}) error {
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
}
if len(values) == 0 {
if len(arguments) != 0 {
return fmt.Errorf("abi: attempting to copy no values while %d arguments are expected", len(arguments))
if len(arguments.NonIndexed()) != 0 {
return errors.New("abi: attempting to copy no values while arguments are expected")
}
return nil // Nothing to copy, return
}
Expand All @@ -137,7 +132,7 @@ func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{
dst := reflect.ValueOf(v).Elem()
src := reflect.ValueOf(marshalledValues)

if dst.Kind() == reflect.Struct && src.Kind() != reflect.Struct {
if dst.Kind() == reflect.Struct {
return set(dst.Field(0), src)
}
return set(dst, src)
Expand Down Expand Up @@ -192,6 +187,9 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
virtualArgs := 0
for index, arg := range nonIndexedArgs {
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
if err != nil {
return nil, err
}
if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) {
// If we have a static array, like [3]uint256, these are coded as
// just like uint256,uint256,uint256.
Expand All @@ -209,9 +207,6 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
// coded as just like uint256,bool,uint256
virtualArgs += getTypeSize(arg.Type)/32 - 1
}
if err != nil {
return nil, err
}
retval = append(retval, marshalledValue)
}
return retval, nil
Expand Down
Loading
Loading